我有这个JSON数据
[
{
"item":{
"name":"some name",
"description":" someDescription ",
"price":"6",
"image_url":"url"
},
"options":[
]
},
{
"item":{
"name":"some name",
"description":" someDescription ",
"price":"6",
"image_url":"url"
},
"options":[
]
}
]
我的代码是
<script id="itemtpl" type="text/template">
{{#items}}
{{#item}}
<div class ="item">
<img src = "{{image_url}} alt="Photo of {{menu_item_name}}"/>
<h3>{{menu_item_name}}</h3>
<h4>{{menu_item_price}}</h4>
<p>{{menu_item_description}}</p>
</div>
{{/#item}}
{{/items}}
</script>
<script type ="text/javascript">
$(function(){
$.getJSON('fullmenu.json', function(data) {
var template = $('#itemtpl').html();
var html = Mustache.to_html(template, data);
$('#menu').html(html);
});//getJSON
});//function
</script>
我使用JavaScript和Mustache模板显示所有项目及其名称,描述和图片。但我无法通过嵌套数组访问。如何检索这些嵌套值?
答案 0 :(得分:1)
如果要迭代顶级数组,则应将其引用为{{#。}} .. {{。/}}} 此外,您还有一些拼写错误,外观,以及您的模板的工作方式:
{{#.}}
{{#item}}
<div class ="item">
<img src = "{{image_url}} alt="Photo of {{name}}"/>
<h3>{{name}}</h3>
<h4>{{price}}</h4>
<p>{{description}}</p>
</div>
{{/item}}
{{/.}}