Render simple array using Mustache.Js

懵懂的女人 提交于 2019-12-05 09:20:21

问题


Having a array like below

var arrNames = ["Stackoverflow","StackExchange","Webmaster","Programmers"];

how should a template look for working with mustache.js javascript template. I tried below but no clues

  • {{#}}{{key}}{{/}}

回答1:


From the documentation:

When looping over an array of strings, a . can be used to refer to the current item in the list.

Template:

{{#musketeers}} * {{.}} {{/musketeers}}

View:

{ "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] }

Output:

  • 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>


来源:https://stackoverflow.com/questions/10045768/render-simple-array-using-mustache-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!