Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It would be much easier if you had your object array like this and use a simple <code>{{#.}} &lt;li&gt;{{name}} -- {{color}} &lt;/li&gt; {{/.}}</code> loop:</p> <pre><code>var myCars = [ {"index":1,"name":"One", "color":"red" }}, {"index":2,"name":"Two", "color":"green" }} ]; </code></pre> <p>But let's hack it hard and this voodoo may help you if you don't want to change the array contents nor traverse it:</p> <p>Example here: <a href="http://jsfiddle.net/9qMqW/2/" rel="nofollow">http://jsfiddle.net/9qMqW/2/</a></p> <pre><code>var myCars = [ {"1":{ "name":"One", "color":"red" }}, {"2":{ "name":"Two", "color":"green" }} ]; // decorate the array with method supporting the exotic indexing myCars.getNextCar = function(){ !myCars.i ? myCars.i=1 : ++myCars.i; return(myCars[myCars.i-1][myCars.i]) }; var template = $("#carsTemplate").html(); $("#carsContainer").html(Mustache.render(template,{cars:myCars})); </code></pre> <p>And your HTML would be:</p> <pre><code>&lt;div id="carsContainer"&gt; &lt;/div&gt; &lt;script id="carsTemplate" type="text/plain"&gt; &lt;ul&gt; {{#cars}} {{#cars.getNextCar}} &lt;li&gt;{{name}} -- {{color}}&lt;/li&gt; {{/cars.getNextCar}} {{/cars}} &lt;/ul&gt; &lt;/script&gt; </code></pre> <p>Note that the method <code>getNextCar</code> it will create an iterator in your object so you have to get a fresh one if you're using more than once. Or just change the array contents as other posters suggested.</p> <p>On a side note, you might want to have a look at <a href="http://handlebarsjs.com/" rel="nofollow">http://handlebarsjs.com/</a> which is a bit more robust with a more flexible syntax.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload