Search code examples
javascriptjqueryhandlebars.js

Handlebars parse error on #each


I'm returning the following JSON object

{"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 ?


Solution

  • 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 replies}}
        /* each stuff */
    {{/each}}