include helper#140
Conversation
this is the include helper as proposed in this pull request of the js implementation: handlebars-lang/handlebars.js#368 the test case which accesses the parent context can't be run yet because the parser will have to be modified and I'll need some help with that once I know if this has a chance of being merged.
|
Hi Chris, If I understand this correctly, the Partials as the Mustache Spec specify should propagate the caller context. Also, since #94 is it even possible to switch the context to anything you want: Examples: Or: Or: So, unless I'm missing something Thanks |
|
To be honest, I'm not sure about the use case with traversing up in the context, I just ported the test until I saw it didn't parse ;) My use case is only about providing static context data to a partial. Our Example: We have breadcrumbs that we sometimes render on the server and sometimes on the client. The first two breadcrumbs are always "Home" and "Back". So if we did this: we would have to make sure that the "home" and "back" items are in the template context on the server and the client, basically duplicating code. With the include, we could instead write this template: Which would keep those two links shared in the template. |
….java during the evaluation of partials * See jknack/handlebars.java#140 * See handlebars-lang/handlebars.js#368 * See handlebars-lang/handlebars.js#182
See associated discussion on js implementation:
And the pull request which I tried to port here:
I think this is a very helpful feature for people sharing templates between .java and .js as it can be used to replace simple helper functions which otherwise would require two implementations (one in java and one in javascript).
Note that it currently only passes a simpler test than the original pull request, because accessing the parent context seems to require a change to the parser which I did not want to dive into before I know that this has a chance of actually going in. I'd be happy to implement it though if you point me to the right place to make the change :)
I also think that the merge method should be refactored to the Context class itself but wanted the change to be minimally invasive for now so people who need this can easily add it to their own code.