Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 683
bigmadwolf Avatar asked Mar 17 '14 22:03

bigmadwolf


2 Answers

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}}
like image 62
76484 Avatar answered Oct 22 '22 13:10

76484


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)

like image 23
Bhushan Pawar Avatar answered Oct 22 '22 15:10

Bhushan Pawar