Let's say I have a element, and inside it want to put an indefinite number of
<ul>
for(i = 0; i < numOfLi; i++)
<li> {{ stuff }} </li>
</ul>
icanhaz (moustache) does include a way to loop.
In javascript:
var listOfStuff = {stuff: [
{key: "1", desc: "First"},
{key: "2", desc: "Second"}
]};
$("#mySelectBox").append(ich.myTemplate(listOfStuff));
In your view:
<script id="myTemplate" type="text/html">
{{#stuff}}
<option value="{{key}}">{{desc}}</option>
{{/stuff}}
</script>
<select id="mySelectBox">
</select>
The {{#stuff}}
and {{/stuff}}
delimit the list. Look at the Sections part of moustache for details.
Edit: Make sure to check out this answer if you're using jQuery 1.9 or above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With