-
Notifications
You must be signed in to change notification settings - Fork 8
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
Support Dynamic Partial Blocks #9
Comments
From a syntax perspective, the decorator syntax that was introduced to support inline partials might be a good fit here. Something like this: {{#*dynamic (partialName)}}
Fallback content
{{/dynamic}} |
Would be interesting to know if this can be implemented as decorator (assuming handlebars-lang/handlebars.js#1372 would be fixed). Edit: No, my comment was dumb. It's not a decorator. If anything, it would be a helper, but helpers cannot easily call partials. |
EDIT: After thinking about this, the following is probably a very bad solution. I'm not sure about side-effects and it uses a lot of API that I wouldn't call public... Something like this could be used to implement a default string for "all" missing partials. const Handlebars = require('handlebars')
Handlebars.registerDecorator('onMissingPartial', function(program, props, container, decoratorContext) {
const invokePartialWrapper = container.invokePartial
container.invokePartial = function (partial, context, options) {
if (props.partials[options.name]) {
return invokePartialWrapper.apply(this, Array.prototype.slice.call(arguments))
}
return decoratorContext.fn(Object.assign({partialName: options.name, context}))
}
});
var template = Handlebars.compile(`
{{#*inline "inlinePartial"}}
inlinePartial
{{/inline}}
{{#*onMissingPartial}}
Partial "{{partialName}}" could not be found
{{/onMissingPartial}}
{{>inlinePartial}}
{{>unknownPartial}}
`)
console.log("output", template({name: 'myPartial2'})) It shows
It does not work with I'm trying to find a way without new language features, because we need a spec first (#1277), and at the moment, nobody seems to be writing one. |
I we would implement this, I think I'd go for the originally suggested |
Are you saying that there's code that already registers the |
You could register it. But Since |
Failover content blocks for dynamic partials would be a fantastic addition to Handlebars. Has there been any movement on this? Is there anything I can do to help push it along? (Also: much ♥ to any/all HBS collaborators for their fantastic work!) |
Also, just came across an SO thread which correctly notes that you can use dynamic partials as block-level helpers today with this ‘lil hack: {{#>( lookup . 'intendedTemplate' )}}
No template matched for "{{intendedTemplate}}"
{{/undefined}} Using |
Sorry for bailing on this without a PR after all the constructive feedback. I got stuck trying to ensure the empty closing block wasn't allowable for other partial blocks, but I went back over everything today and think it's now working as intended in handlebars-lang/handlebars.js#1422. |
Thank you for the PR, @jstewmon. I think the final decision has to be made by @wycats because this is definitely a language enhancement and he asked me only to fix bugs. I'm not certain about his intended direction for the language.
|
Implemented in handlebars-lang/handlebars.js#1422 but not merged yet |
Any progress on this yet? |
Currently, there is no way of defining a dynamic partial with block syntax, so that a failover can be provided if the sub expression resolves to an undefined partial.
Why is this important?
When we use a dynamic partial block, the model-building code may not be aware of which partials are registered. For example, we may register partials with names that correspond to error codes, so that we can render an error-specific template in some cases, falling back to a default template. So, it would be convenient to supply a default template to use when the dynamic partial does not resolve to a registered partial.
I found that it is fairly trivial to support this behavior if the
closeBlock
helperName
is optional:Of course, this solution allows closing any block with
{{/}}
, which would be inadvisable as a regular practice, but doesn't implicitly harm anything.validateClose
could include an additional check to only allow the empty close whenopen.type === 'SubExpression'
, so that only a dynamic partial block is allowed to have an empty close (I think that's the only case when the open will be of typeSubExpression
).Another option is to allow
partialName
in thecloseBlock
:I don't think this would be a very good solution in practice because dynamic partial expressions are rarely so trivial, so matching the sub expression in the
openPartialBlock
andcloseBlock
would be quite tedious.I wanted to find a way to label an
openPartialBlock
containing a sub expression, but couldn't figure out a way of doing so that is clearly disambiguated from the existing partial syntaxes. I'd love to hear other syntactical suggestions, and I'd be happy to submit a PR if there consensus on a syntax.The text was updated successfully, but these errors were encountered: