-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Is it possible to render a partial without the parent scope? #1032
Comments
Here's my current workaround: Handlebars.registerHelper('component', (partial, options) => {
const template = Handlebars.compile(Handlebars.partials[partial])
const html = template(options.hash)
return new Handlebars.SafeString(html)
}) {{component "myPartial" name="Adam"}} |
Passing a first argument to the partial call will render in its own context, independent of the parent. I.e. {{> myPartial newContext name="foo"}} |
Why is that not the default behavior? That was a rude shock when I found out. |
How exactly would we default to providing a context from an arbitrary variable without the author selecting that variable by name? |
I'm not sure I understand. If a set of key-value pairs were provided {{> radioButton name="enabled" value="true" id="enableUser" The object used as a context would be: I don't know how it works now internally (I'm just a user). On 8/7/15, Kevin Decker [email protected] wrote:
|
@rayHInstart Those keys are added to the existing context to form a new context that has both sets of data, with the explicitly passed hash parameters given priority. This has a performance impact vs. some of the alternatives, but outside of that I'm not sure what was quite so rude or shocking. Glad to discuss any changed to behavior you'd like to see but please outline it fully as well as outline the issues that you've seen from the existing behavior. |
In my use case, I had a partial which used a common variable name,
{{#if disabled}}Hi, I'm disabled{{/if}}
{{#unless disabled}}I'm super active!{{/unless}}
And I used it like this: , expecting that since I did not explicitly pass a disabled into the On 8/12/15, Kevin Decker [email protected] wrote:
|
Regarding documentation, the behavior is covered here but as with all documentation, what makes sense to someone who already knows the code is not always the same for others.
I did not write the original implementation of this so I can't really comment on why the decisions were made as they were, but I can say that changing this is difficult. Many of these arguments are completely valid but like most issues filed with Handlebars of late, counter arguments can be made to equal degree, and for those cases "don't break existing code" generally wins. I don't think it will be much overhead to add a flag that requires explicit contexts to be passed for partials which I can look at for this release, but we aren't going to be able to change the existing behavior. This does little to account for confusion of users just starting with the language, but would allow for your use case with out nasty temporary objects or similar hacks. |
Thanks Kevin. "Don't break existing code" is indeed a winning On 8/14/15, Kevin Decker [email protected] wrote:
|
Released in 4.0.0 |
To reference
|
Is it possible to get a reference to the local context? If we have clashing name from some parent context, we could just reference a local context specifically inside a partial: |
local context variable still do not exist ? I would just like to access hash arguments like it's possible in a helper with options.hash. How to get only the hash arguments in a partial ? |
I'm trying to treat partials as "components" that only have access to variables that were passed in as attributes — is this possible?
my-partial.hbs
my-app.js
my-app.hbs
Actual output
Desired output
The text was updated successfully, but these errors were encountered: