Context resolver and partials

60 views
Skip to first unread message

Oliver

unread,
Mar 2, 2016, 8:28:00 AM3/2/16
to handlebars.java
I just ran into something unexpected. If I set up a context like this:

Context context = Context.newBuilder(contextData)
.resolver(JsonNodeValueResolver.INSTANCE_.build()

And then use that to apply to a template that includes a partial with values like:

{{> other key="value" }}

The value of key is not available in the "other" template. However, if I do this:

Context context = Context.newBuilder(contextData)
.resolver(JsonNodeValueResolver.INSTANCE,
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE).build()

everything works as expected. From the documentation and the examples I assumed that new Context objects in the stack would have their own, default resolver. If nothing else, docs like these: https://jknack.github.io/handlebars.java/jackson.html don't mention that messing with the resolver will potentially break partial. Also, the fix above seems brittle as I have hard coded the default resolvers. Should those ever change my templates would break. I'd think a "combineResolver" method which is the analog of the "combine" method on the context builder would be a nice addition.

So, just wanted to double check, is the fix above the right one? Any way to protect myself against the addition, removal, or change of the default resolvers in the future?

Thanks,
Oliver

edgar

unread,
Mar 2, 2016, 10:14:29 AM3/2/16
to handlebars.java
Yes, it is the right way of doing it.

The MapValueResolver is more or less ALWAYS required... and as you already noticed the partial arg/hash is implemented with a HashMap.

Agreed that a combineResolver will be a nice addition. Can you file an issue at github?


Thanks and I invite you to try: http://jooby.org 

Oliver

unread,
Mar 2, 2016, 1:20:00 PM3/2/16
to handlebars.java
Reply all
Reply to author
Forward
0 new messages