【问题标题】:Handlebar template handling with numbers带数字的车把模板处理
【发布时间】:2014-01-04 12:26:14
【问题描述】:

我有一个类似

的 JSON 字符串
{
    "2013": [{
        "01": {.... },
        "01": {.....},
        "02": {.....}
    }]
}

我有一个这样的模板

<script id="thumbnail" type="text/x-handlebars-template">
    {{#2013}}
        <div>Content</div>  
    {{/2013}}           
</script>

但这不起作用,因为循环块就像{{#2013}}。如果我把它改成一些字符串,比如{{#thumbnails}},它就完美了。

有没有办法让 JSON 保持原样?

【问题讨论】:

  • 使用#each 方法来迭代你的数据。例如 ul> {{#each 2013}}
  • {{agree_button}}
  • {{/each}}

标签: handlebars.js template-engine


【解决方案1】:

#with 助手呢?

<script id="thumbnail" type="text/x-handlebars-template">
    {{#with 2013}}
    <div>Content</div>
    {{/with }}
</script>

<script type="text/javascript">
    var context = { "2013": [{ "01": "..", "01": "..", "02": ".." }] };
    var source = $("#thumbnail").html();
    var template = Handlebars.compile(source);
    var html = template(context);
    $("#result").html(html);
</script>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签