I'm using handlerbars to create templates. Suppose that I'm doing a TODO list. I have a collection and I need also support for adding new TODO elements with the same style.
So far I have a TODO template collection:
<script id="TODO-collection-templ" type="text/x-handlerbars-template">
<div id="collection">
<ul>
{{#list todos}}
<li><span>{{this.title}}</span><span>{{this.description}}</span></li>
{{/list}}
</ul>
</div>
</script>
If I want to add new elements, the only way (to me) it would be creating another template that builds the following:
<script id="TODO-templ" type="text/x-handlerbars-template">
<li><span>{{title}}</span><span>{{description}}</span></li>
</script>
So I end up having two templates but those are prone to errors (If I change something in TODO-collection-templ and I forget to do the same change over the TODO-templ, it will not render the Html properly)
Is there any way to include the TODO-templ inside the TODO-collection-templ ??
Handlebars: What it is and Why to Use itHandlebars is a logic-less templating engine that dynamically generates your HTML page.
Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.
There are partials in Handlebars like in Mustache's: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers
Basically you can organise your micro-template as a partial and use it in the bigger template:
<script id="TODO-partial" type="text/x-handlebars-template">
<li><span>{{title}}</span><span>{{description}}</span></li>
</script>
<script id="TODO-collection-templ" type="text/x-handlebars-template">
<div id="collection">
<ul>
{{#list todos}}
{{> TODO}}
{{/list}}
</ul>
</div>
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With