Object traversal in handlebars js vs handlebars java

507 views
Skip to first unread message

am80l

unread,
Nov 10, 2014, 10:30:13 PM11/10/14
to handleb...@googlegroups.com
I ran into a snag trying to share some templates between handlebars.js and handlebars java. 

handlebars.js requires a relative traversal up to the parent object when accessing an object property from within a block helper.
 
Handlebars java apparently will recursively look up the parent context for a property if it is not found in the current context, which is great, however since I am sharing templates I need to have consistent syntax.
 
So i tried using ../ to access parent properties within a block in handlebars.java, and it worked. However when i needed to go two parents up using ../../ it did not work at all.

Example:

{{#each foo.bars}}  
    
{{../foo.prop}} // works in both handlebars.js and handlbars java
    
{{if ../foo.cond}}  
        
{{../../foo.prop}} // does not work in handlebars java, only works in handlebars.js
    
{{/if}}
{{/each}}      


Any recommendations on how to solve this? It seems like the ../../ should still work in handlebars java. Is this a bug? 

edgar

unread,
Nov 11, 2014, 6:53:56 AM11/11/14
to handleb...@googlegroups.com
Looks like a bug, but to be 100% sure can you share your data? You can describe it JSON if you want too.

Then, handlebars.java is a mustache compliant spec that is why it looks up the parent content (recursively). Handlebars.js, isn't a mustache compliant for the same reason.

am80l

unread,
Nov 11, 2014, 4:43:12 PM11/11/14
to handleb...@googlegroups.com
Absolutely. I put together 2 small examples. While putting them together I found whats actually happening. It appears the ../../ works in the context of a nested each, so the problem is not with the traversal. The problem is is with the if statements. In handlebars js, an if block will create a new level of context, thus you'll need ../ to get back to the parent from within the if block. It appears handlebars java does not create this extra level of context and thus traversing out of the block does not work. This appears to be the real issue. 

Here's my code samples:

Data:
{
    "prop":"foo",
    "objects":{
       "prop":"foo",
       "prop2":"bar"
   }
}

hbs: working example of nested each
{{#each hbstest.objects}}
    {{#each ../hbstest.objects}}
          {{../../hbstest.prop}}   {{!--works in hbs java and handlebars.js--}}
    {{/each}}
{{/each}}

hbs: doing the same within an if statement breaks handlebars java
{{#each hbstest.objects}}
    {{../hbstest.prop}} {{!--works in hbs java and handlebars.js--}}
    {{#if ../hbstest.objects}} {{!-- conditional fires in both hbs java and handlebars.js--}}
        {{../../hbstest.prop}} {{!--only resolves in handlebars.js--}}
    {{/if}}
{{/each}}

am80l

unread,
Nov 11, 2014, 4:48:16 PM11/11/14
to handleb...@googlegroups.com
Just to clarify, in the above example the data object was loaded with the name hbstest. 

edgar

unread,
Nov 23, 2014, 7:22:46 PM11/23/14
to handleb...@googlegroups.com
hey, can you raise this at github? https://github.com/jknack/handlebars.java/issues/new

thanks
Reply all
Reply to author
Forward
0 new messages