Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include helper #140

Merged
merged 1 commit into from
Mar 23, 2013
Merged

include helper #140

merged 1 commit into from
Mar 23, 2013

Conversation

c089
Copy link
Contributor

@c089 c089 commented Mar 20, 2013

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.

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.
@jknack
Copy link
Owner

jknack commented Mar 20, 2013

Hi Chris,

If I understand this correctly, the include helper isn't necessary. Here is an explantation.

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:

  {{> partial}} same as {{> partial this}}

Or:

  {{> partial context}}

Or:

  {{> partial ../}}

So, unless I'm missing something include helper is just an alias for native partials, right?

Thanks

@c089
Copy link
Contributor Author

c089 commented Mar 20, 2013

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:

{{#each breadcrumbs}}
  {{>breadcrumb}}
{{/each}}

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:

{{breadcrumb title="Home" url="http://example.com/"}}
{{breadcrumb title="I don't know my browser has a back button" url="javascript:history.back()"}}
{{#each breadcrumbs}}
  {{>breadcrumb}}
{{/each}}

Which would keep those two links shared in the template.

jknack added a commit that referenced this pull request Mar 23, 2013
@jknack jknack merged commit 6025f5f into jknack:master Mar 23, 2013
@jknack jknack mentioned this pull request Aug 1, 2013
jon-bell pushed a commit to FlakyTestDetection/handlebars-java that referenced this pull request Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants