arrays - 使用 Mustache.Js 渲染简单数组

标签 arrays mustache

有一个像下面这样的数组
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/

    相关文章:

    JavaScript:使用 Array 方法编写多个函数

    ruby - 如何在ruby中获取字符串数组的总和

    node.js - 尝试在 Express JS 中使用 Mustache 时出现 TypeError : this. 引擎不是函数

    javascript - 如何缓存 mustache 模板?

    amazon-web-services - 使用 Cloudformation 和 Pystache 派生 IP 范围和网络掩码

    java - 将 int 数组的内容复制到 Java 中的 double 组?

    java - 可变参数和 '...' 参数

    java - 在java中修改复制的二维数组

    ios - GRMustache 在过滤器中传递常量值?

    javascript - 限制列表表达式中显示的列表项数量