用Mustache为每个人做一个

Make a for-each with Mustache?

本文关键字:一个 Mustache 每个人      更新时间:2023-12-18

我试图学习Mustache,但无法让这个脚本发挥作用,而且我在谷歌上找到的指南似乎没有涵盖这个。

<div id="test"> </div>
<script id="testtpl" type="text/template">
    {{#sheet}}
      {{#.}}    
        {{a}}
      {{/.}}
    {{/sheet}}         
</script>
<script>
  var testing = {
    sheet: {
      'fur': {a: 6, b: 2, item: ['bold']},
      'bur': {a: 6, b: 2, item: ['bold']}
    }
  };
  $(function() {
    var template = $('#testtpl').html();
    var html = Mustache.to_html(template, testing);
    $('#test').html(html);
  });
</script>

我想你想要这样的

Handlebars模板

{{#eachProp sheet}}
  {{this}}
{{/eachProp}}

上下文或javascript文本

{
    sheet: {
      'fur': {a: 1, b: 2, item: ['bold']},
      'bur': {a: 5, b: 2, item: ['bold']}
    }
}

把手助手

Handlebars.registerHelper('eachProp', function(context, options) {
  var data,
    out = [];
   console.log(context);
   for (var key in context) {
     out.push(context[key].a);
   }
   out = out.join(',');
  return out;
});

在Try Handlebars上尝试这些块。现在在网站上玩这个,得到你想要的任何东西。希望这能有所帮助!!