【问题标题】:how to use ICanHaz for moustache javascript template engine?如何将 ICanHaz 用于 mustache javascript 模板引擎?
【发布时间】:2012-05-31 10:29:55
【问题描述】:

我有一个 JSON 字符串:

{
  "items": [
      {"name": "red" },
      {"name": "blue" }
  ],
  "test" : {
        "items" :[
             { "name" : "Hello" },
             { "name" : "World" }
         ]
  }
}

如何打印出来

<li>Hello</li>
<li>World</li>

我尝试使用下面的模板,但它不起作用。它改为打印“红色和蓝色”。我无权更改 JSON 字符串,我只能操作模板。

{{#test}}
  {{#items}}
     <li>{{name}}</li>
  {{/items}}
{{/test}}

【问题讨论】:

    标签: template-engine icanhaz.js


    【解决方案1】:

    由于某种原因,下面的代码:

    <head>
    <script src="https://github.com/andyet/ICanHaz.js/raw/master/ICanHaz.js"></script>
    
    <script>
    function clicked()
    {
           ich.addTemplate("user", "{{#test}} {{#items}} <li>{{name}}</li>\n {{/items}} {{/test}}");
           document.getElementById("result").innerHTML = ich.user(userData);
    }
    
    var userData = {
      "items": [
          {"name": "red" },
          {"name": "blue" }
      ],
      "test" : {
            "items" :[
                 { "name" : "Hello" },
                 { "name" : "World" }
             ]
      }
    };
    
    </script>
    
    </head>
    <body>
        <button onclick="clicked()">CLICK</button>
        <ul id="result"><li>Result</li></div>
    </body>
    

    准确地告诉我:

    • 你好
    • 世界

    所以,你的模板应该是正确的。

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 2011-01-05
      • 2012-03-06
      • 1970-01-01
      相关资源
      最近更新 更多