Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache(icanhaz) iterating array of arrays

In mustache if we have an array like :

var a = [1,2,3,4];

We can create template like:

{{#a}}
{{.}}
{{/a}}

to iterate over it. Now if we have some thing like

var a = [[1,2], [3,4], [5,6]]

Can we create a template like:

{{#a}}
key is {{0th element}} and the value is {{1st element}}
{{/a}}
like image 843
aditya_gaur Avatar asked Mar 06 '12 11:03

aditya_gaur


1 Answers

Tried out things and got the solution: We can do the following:

var htm = '{{#names}}'+
            '<p> value="{{0}}" key = "{{1}}"</p>'+
          '{{/names}}';
ich.addTemplate('formNameOptionsHTML',htm);
var arr =[[0,1],[10,11],[20,21]];
var htm = ich.formNameOptionsHTML({names:arr});
$('body').append(htm);

Here is the jsfiddle link to it.

like image 124
aditya_gaur Avatar answered Sep 21 '22 15:09

aditya_gaur