最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HandleBars - template inside templates - Stack Overflow

programmeradmin2浏览0评论

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 ??

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 ??

Share Improve this question edited Feb 6, 2017 at 11:46 fury.slay 1,2581 gold badge16 silver badges26 bronze badges asked Nov 30, 2013 at 11:03 kitimenpolkukitimenpolku 2,6044 gold badges39 silver badges58 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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>
发布评论

评论列表(0)

  1. 暂无评论