Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repetitive blocks #34

Open
oravecz opened this issue Feb 10, 2017 · 4 comments
Open

Repetitive blocks #34

oravecz opened this issue Feb 10, 2017 · 4 comments

Comments

@oravecz
Copy link

oravecz commented Feb 10, 2017

If I have a repetitive block in my template (aka partial), is it possible to define it and reference it from multiple locations in the template?

@davidjamesstone
Copy link
Owner

davidjamesstone commented Feb 12, 2017

I usually go with creating a separate child template, compile it to IDOM then pass it in to the parent. Using browserify with the superviewify transform it could look something like this:

child.html

<span>I'm from the child</span>

parent.html

<template args="data childView">
<span>I'm from the parent</span>
<script>
  childView(data)
</script>
</template>

main.js

const childView = require('./child.html')
const parentView = require('./parent.html')

patch(element, function (data) {
  parentView(data, childView)
}, data)

This works and is fairly simple but if you're looking for something more you could use customElements as part of the web components spec. skatejs is worth looking at. I works with IDOM, supports superviews.js and would allow you to simplify your code to:

<x-parent>
  <x-child></x-child>
  <x-child></x-child>
</x-parent>

Hope that helps?

@oravecz
Copy link
Author

oravecz commented Feb 13, 2017 via email

@oravecz
Copy link
Author

oravecz commented Feb 13, 2017

Something like this would be helpful in my use case.

template.html

<template name="child" args="item index">
    <span class="{index % 2 === 0 ? 'even' : 'odd'}">
        {item.name} :: {index}
    </span>
</template>

<template name="parent" args="items">
    <style>
        .odd {
            background-color: #ccc;
        }
    </style>
    <h2>Partial example</h2>
    <ul>
        <li each="item in items">
            { child(item, $index) }
        </li>
    </ul>
</template>

You could default to your current behavior in case of a single template in a file, but perhaps export either the last template when there are multiples, or export an object with each factory function exposed as a property.

Perhaps nested template tags would be cleaner in regards to how you export a single function.

<template name="parent" args="items">
    <template name="child" args="item index">
        <span class="{index % 2 === 0 ? 'even' : 'odd'}">
            {item.name} :: {index}
        </span>
    </template>
    <style>
        .odd {
            background-color: #ccc;
        }
    </style>
    <h2>Partial example</h2>
    <ul>
        <li each="item in items">
            { child(item, $index) }
        </li>
    </ul>
</template>

@robhicks robhicks mentioned this issue Feb 16, 2017
@rogi29
Copy link

rogi29 commented Aug 21, 2017

I find it an issue as well, where I want to include multiple templates using an html like syntax to create modular components. Is there a possibility to extend the handler function, therefore allowing us to create custom tags like include or component?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants