Mustache templates: How to output a block only once for non-empty lists

前端 未结 3 826
傲寒
傲寒 2020-12-18 21:16

If my list is empty, I want to output this:

If my list i

3条回答
  •  执笔经年
    2020-12-18 22:01

    You could use non-false values of a section. name would have to be an object inside of items

    data = { 'items' : { 'name' : ["Item 1", "Item 2", "etc"] } };
    

    You template would look like:

    {{#items}}
      {{#name}}
    • {{.}}
    • {{/name}}
    {{/items}}

    Here's an example on jsFiddle showing the rendering of an items object with names and without -- http://jsfiddle.net/maxbeatty/fpQwk/

提交回复
热议问题