Can mustache iterate a top-level array?

前端 未结 5 877
刺人心
刺人心 2020-12-04 08:21

My object looks like this:

[\'foo\',\'bar\',\'baz\']

And I want to use a mustache template to produce from it something like this:

5条回答
  •  情话喂你
    2020-12-04 09:11

    Following are the examples to render multi-dimensional array in a template:

    Example 1

    'use strict';
    
    var Mustache = require('mustache');
    
    var view = {test: 'div content', multiple : ['foo', 'bar'], multiple_2 : ['hello', 'world']};
    var template = '
    {{test}}
      {{#multiple}}
    • {{.}}
    • {{/multiple}}
      {{#multiple_2}}
    • {{.}}
    • {{/multiple_2}}
    '; var output = Mustache.render(template, view); console.log(output);

    Example 2

    'use strict';
    
    var Mustache = require('mustache');
    
    var view = {test: 'div content', multiple : [{name: 'foo', gender: 'male'}, {name: 'bar', gender: 'female'}], multiple_2 : [{text: 'Hello', append: '**', prepend: '**'}, {text: 'World', append: '**', prepend: '**'}]};
    var template = '
    {{test}}
      {{#multiple}}
    • Hello my name is {{name}}. And I am {{gender}}
    • {{/multiple}}
      {{#multiple_2}}
    • {{prepend}}_{{text}}_{{append}}
    • {{/multiple_2}}
    '; var output = Mustache.render(template, view); console.log(output);

    For test run, save above examples in file called 'test.js', run following command in commandline

    nodejs test.js
    

提交回复
热议问题