Javascript 如何使用 mustache js 模板引擎访问嵌套对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8912540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to access nested objects with mustache js templating engine
提问by ptamzz
I've this json return
我有这个 json 回报
{
"timeline": [{
"id": "2",
"self": {
"uid": "2",
"username": "ptamzz"
},
"file": {
"fid": "43",
"file_name": "First Name"
},
"connection": {
"fid": "4",
"username": "tom"
},
"action": "viewed your document",
"time": "2012-01-16 12:23:03",
"tags": ["Engineering", "Computer Science", "Java", "Java Library"]
}, {
"id": "1",
"self": {
"uid": "2",
"username": "ptamzz"
},
"file": {
"fid": "41",
"file_name": "Write Up"
},
"connection": {
"fid": "4",
"username": "tom"
},
"action": "favorited your document",
"time": "2012-01-16 12:22:04",
"tags": ["Design"]
}]
}
According to the tutorial at http://coenraets.org/blog/2011/12/tutorial-html-templates-with-mustache-js/(Sample 6: Nested Object section), you can access dot notation
to access the nested objects.
根据http://coenraets.org/blog/2011/12/tutorial-html-templates-with-mustache-js/上的教程(示例 6:嵌套对象部分),您可以访问dot notation
嵌套对象。
From the above json, I want to retrieve the data like self.username
, file.file_name
etc etc.
从上面的JSON,我想要检索的数据等self.username
,file.file_name
等等等等。
Now, I've my template as
现在,我有我的模板
{{#timeline}}
<li>
{{self.username}}
</li>
{{/timeline}}
But self.username
doesn't work.
但self.username
不起作用。
How do I retrieve these nested values?
如何检索这些嵌套值?
回答by ptamzz
I don't think it's the right way to do but since I couldn't find any answers here, I figured out something myself. At least this works.
我不认为这是正确的做法,但由于我在这里找不到任何答案,所以我自己想出了一些办法。至少这是有效的。
{{#timeline}}
<li>
{{#self}}{{username}}{{/self}}
</li>
{{/timeline}}
回答by d1val
Dot notation does not work on version 0.4x and below. It worked on "0.7.2".
点表示法不适用于 0.4x 及以下版本。它适用于“0.7.2”。