有一个像下面这样的数组var arrNames = ["Stackoverflow","StackExchange","Webmaster","Programmers"];
模板应该如何与 mustache.js javascript 模板一起使用。我在下面试过但没有线索
{{#}}{{key}}{{/}}
最佳答案
来自 documentation :
When looping over an array of strings, a . can be used to refer to the current item in the list.
Template:
{{#musketeers}} * {{.}} {{/musketeers}}
看法:
{ "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] }
输出:
- Athos
- Aramis
- Porthos
- D'Artagnan
var tpl = document.getElementById('simple').innerHTML, view = { items: ['Stackoverflow', 'StackExchange', 'Webmaster', 'Programmers'] }; document.getElementById('output').innerHTML = Mustache.to_html(tpl, view);
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.0/mustache.js"></script> <script type="template" id="simple"> <h1>Array Values</h1> <ul> {{#items}} <li>{{.}}</li> {{/items}} </ul> </script> <div id="output"></div>
关于arrays - 使用 Mustache.Js 渲染简单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10045768/