English 中文(简体)
如何使用 ICanHaz 做胡子刺绣模板引擎?
原标题:how to use ICanHaz for moustache javascript template engine?

我有一个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}}
问题回答

出于某种原因,以下代码:

<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>
 {{/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>

确切地说 :

  • Hello
  • World

所以,你的模板应该是对的。





相关问题
Template engines [closed]

Are there any for purposes other than web? e.g. for use in script generators, c++ code generators or other related, generic operations.

Is it there a Ruby equivalent of Java s Wicket?

The idea would be to replace ERB with templates that are pure XHTML and that the view would be pure code manipulating the template content. Have this been done already ?

non-servlet JSP

I realise there are EL and near EL frameworks like StringTemplate, JUEL, Velocity, etc. However, I am not asking for list of alternatives to JSTL/EL. I came across a package more than a year ago, ...

More descriptive Language String placeholder?

<?LANG(no_download, you are not allowed to download ) instead of $lang[no_download] I have what I think is a better approach for embedding language strings in templates. In almost all the PHP ...

Python for web scripting

I m just starting out with Python and have practiced so far in the IDLE interface. Now I d like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, ...

How to use the John Resig JavaScript Micro-Templating engine?

I have searched for a bit on a basic example on using the John Resig JavaScript Micro-Templating engine on Google but came out dry. I decided to bring it to base guys. Can anyone help with a simple ...

热门标签