基本用法:
html
var html = Mustache.to_html(template, data); $('#content').html(html)
總結一句話,若是傳入的data是個數組,用點表示這個無名的數組。
而{{#blah}} ... {{/blah}}是被重載過的,碰到數組就會遍歷,碰到對象就會進去。
##帶key的 json
The array itself should be a value in a bigger Object, like so: 數組
var obj = { arr: [ {"id": "1", "details": {"name": "X", "type":"Y" }}, {"id": "2", "details": {"name": "aName", "type":"something" }} ] }
Then you can do: less
{{#arr}} my id: {{id}} {{/arr}}
the use for {{#bla}} is actually overloaded. When the property it acts upon is an array it will loop. When the property is an object itself it will 'enter' that object. Given your example: oop
{{#arr}} my id: {{id}} <br/> {{#details}} and my name: {{name}} {{/details}} {{/arr}}
##keyless不帶key的(使用.語法) this
I found another way of doing this which is pretty similar to Geert-Jan except that you do not have to assign the array to a Javascript object. spa
{{ #. }} <p> {{ id }} </p> <ul> {{#details}} {{name }} {{ type }} {{/details}} </ul> {{ /. }}