Render simple array using Mustache.Js

后端 未结 1 592
轻奢々
轻奢々 2021-02-11 21:28

Having a array like below

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

how should a template look for workin

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-11 22:28

    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);
    
    
    
    

    0 讨论(0)
提交回复
热议问题