Handlebars在#each上解析错误

时间:2014-03-17 22:09:23

标签: javascript jquery handlebars.js

我正在返回以下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":[]
}

我在我的车把模板中收到以下错误:

Uncaught Error: Parse error on line 23:
...replies}}                    {{ #each replies }
----------------------^
Expecting 'ID', 'DATA', got 'INVALID' 
在我的tempalte里面,我做了以下事情:

        {{#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}}

回复当前是空的,但是当回复消息时它应该返回一组对象,1)最好的方法1)使这个工作和2)使用handlebars.js处理这种数据结构?

2 个答案:

答案 0 :(得分:21)

你得到的错误是因为你在'#each'和'/ each'之前有一个空格,所以要做到:

{{#each replies}}
    /* each stuff */
{{/each}}

答案 1 :(得分:2)

您也可以直接使用如下方式来降低复杂性,如果每个块有多个,您也可以很好地理解。

<style>

此方法与{{#replies}} {{this}} {{/replies}} 并行,如果 object ,则可以迭代键值,否则数组索引和对象(值)