【问题标题】:Handlebars partials name in a jsonjson中的车把部分名称
【发布时间】:2013-06-01 08:19:03
【问题描述】:

我有那个车把模板:

{{#each slides}}
    <div class="slide">
        {{> }}
    </div>
{{/each}}

在哪里有 {{> }} ,我想渲染一个部分谁的名字来自与模板关联的数据。 比如这个数据:

{
    slides : [
        {
            id : "id",
            slidePartial : "1-textleft"
        },

        {
            id : "id1",
            slidePartial : "2-textright"
        }
    ]
}

slidePartial 将是动态加载的部分,所以我们会有类似的内容:

    <div class="slide">
        {{> 1-textleft}}
    </div>
    <div class="slide">
        {{> 1-textright}}
    </div>

我已经读到这是不可能的,因为模板是之前编译的,那么我该怎么做呢?如果可能的话。

谢谢。

【问题讨论】:

    标签: javascript templates partials


    【解决方案1】:

    我通过这个出色的 Handlebars 助手找到了解决方案:Conditional partials in Handlebars

    所以,在我的主模板中,我有类似的内容:

    {{#compare slidePartial '1-textleft' operator='==='}}
      {{> 1-textleft this.content}}
    {{/compare}}
    
    {{#compare slidePartial '2-textright' operator='==='}}
      {{> 1-textright this.content}}
    {{/compare}}
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多