In Mustache, How to get the index of the current Section

前端 未结 13 1113
臣服心动
臣服心动 2020-12-13 03:52

I am using Mustache and using the data

{ \"names\": [ {\"name\":\"John\"}, {\"name\":\"Mary\"} ] }

My mustache template is:



        
13条回答
  •  醉话见心
    2020-12-13 04:19

    This is how I do it in JavaScript:

    var idx = 0;
    
    var data = { 
       "names": [ 
           {"name":"John"}, 
           {"name":"Mary"} 
        ],
        "idx": function() {
            return idx++;
        }
    };
    
    var html = Mustache.render(template, data);
    

    Your template:

    {{#names}}
        {{name}} is {{idx}}
    {{/names}}
    

提交回复
热议问题