gpt4 book ai didi

javascript - 如何为带有嵌套对象的 JSON 数据创建 Mustache 模板?

转载 作者:行者123 更新时间:2023-12-03 11:02:14 24 4
gpt4 key购买 nike

我有这个 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 模板来显示所有项目及其名称、描述和图片。但我在通过嵌套数组访问时遇到问题。如何检索这些嵌套值?

最佳答案

如果您想迭代顶级数组,您应该将其引用为 {{#.}} .. {{./}}另外,您还有一些拼写错误,看,这就是您的模板的工作方式:

{{#.}}
{{#item}}
<div class ="item">
<img src = "{{image_url}} alt="Photo of {{name}}"/>
<h3>{{name}}</h3>
<h4>{{price}}</h4>
<p>{{description}}</p>
</div>
{{/item}}
{{/.}}

关于javascript - 如何为带有嵌套对象的 JSON 数据创建 Mustache 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com