javascript 把手解析 #each 上的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22466526/
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
Handlebars parse error on #each
提问by bigmadwolf
I'm returning the following JSON object
我正在返回以下 JSON 对象
{"status":"success",
"valmessage":"Your message has been sent successfully.",
"postcontent":{
"post_author":1,
"post_recipient":"1",
"post_title":"handle test 2",
"post_type":"wp_inbox_msg",
"post_content":"<p>giving it another go.<\/p>\n"},
"postmeta":{
"postid":410,
"postdate":"Monday, March 17th, 2014",
"author":"admin"},"replies":null,
"Formerrors":[]
}
and I'm getting the following error inside my handlebars template :
我的车把模板中出现以下错误:
Uncaught Error: Parse error on line 23:
...replies}} {{ #each replies }
----------------------^
Expecting 'ID', 'DATA', got 'INVALID'
inside my tempalte I'm doing the following :
在我的模板中,我正在执行以下操作:
{{#if replies}}
{{ #each replies }}
<li>
<figure class="reply-author-img">
<img alt="" src="{{ avatar }}" height="96" width="96">
</figure>
<div class="replycontentwrap">
<div class="reply-from">
<p class="reply-author">
<strong>{{ fullname }}</strong>
<span>{{ nickname }}</span>
</p>
<span class="replydate">{{ reply_date }}</span>
</div>
<div class="reply-content">{{ reply_content }}</div>
</div>
</li>
{{ /each }}
{{/if}}
Replies is currently Null but it should be returning a set of objects when there are replies to the message, How would be the best way to 1) get this working and 2) approach this kind of data structure with handlebars.js ?
Replies 目前为 Null,但当有消息回复时,它应该返回一组对象,如何最好地 1) 使其正常工作和 2) 使用 handlebars.js 处理这种数据结构?
回答by 76484
The error you are getting is a result of the fact that you have a space before the '#each' and '/each', so make it:
你得到的错误是因为你在 '#each' 和 '/each' 之前有一个空格,所以让它:
{{#each replies}}
/* each stuff */
{{/each}}
回答by Bhushan Pawar
You can also directly use as below to reduce complexity, also you get good understanding if there are multiple each blocks.
您也可以直接使用如下来降低复杂度,如果每个块有多个,您也可以很好地理解。
{{#replies}}
{{this}}
{{/replies}}
This method is parallel to #each
and can iterate key value if it is object, otherwise array index and object(value)
#each
如果是object,则此方法与并可以迭代键值,否则数组索引和 object(value)