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 and reduce the breach between Handlebars.js/Handlebars.java during the evaluation of partials #141

Closed
jknack opened this issue Mar 23, 2013 · 0 comments
Milestone

Comments

@jknack
Copy link
Owner

jknack commented Mar 23, 2013

This is more or less a workaround for Handlebars.js to allow access to parent context from partials.

The Mustache Spec said that if an attribute is missing in the current scope, the parent scope need to resolve it, until the attribute is found or we are at the root context.

context = this;
while(context && !context.get("attribute")) {
  context = context.parent;
}
attribute = context? context.get("attribute"): null;

The Mustache Spec also said that the current context must be propagated to partials.

These are some examples where all they produce the same output:

Access to the parent context without partials.

{{#each hobbies}}
  {{../name}} has hobby {{hobbyname}} and lives in {{../town}}
{{/each}}

Supported by: Handlebars.js and Handlebars.java

Access to the parent context from partial using include helper

hobby partial

{{parentcontext.name}} has hobby {{hobbyname}} and lives in {{town}}
{{#each hobbies}}
{{include \"hobby\" parentcontext=.. town=../town}} 
{{/each}}

Supported by: Handlebars.js once #368 get merge and Handlebars.java

Access to the parent context from partials using Mustache Spec:

hoby partial

{{name}} has hobby {{hobbyname}} and lives in {{town}}
{{#each hobbies}}{{> hobby}} {{/each}}

Supported by: Mustache Spec and Handlebars.java

Explicit access to the parent context from partials using Mustache Spec and Handlebars parent reference:

hoby partial

{{../name}} has hobby {{hobbyname}} and lives in {{../town}}
{{#each hobbies}}{{> hobby}} {{/each}}

Supported by: Handlebars.java

So, I don't think we need the include helper but I'm going to keep it until Handlebars.js implement attributes resolution as the Mustache Spec said.

The only nice feature of include is the ability of create/override attributes using hashes, like:

{{#each hobbies}}{{include "hobby" name="Edgar"}} {{/each}}

See #140 for more details

See #368 for more details

See #182 for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant