Throw error if value used with `#with` is falsy #770
Comments
I've also encountered problems with this. I wonder if there would be a significant downside to having the {{#with}} I think this behavior would parallel the other default block helpers in a On Sat, Mar 2, 2013 at 6:00 PM, Mathieu Bouchard
|
@belisarius222 I agree with your suggestion |
@belisarius222, @glasser and I like your proposal too. |
Cool! Thanks for pointing me in the right direction. I'll keep you posted. On Tue, Mar 5, 2013 at 12:00 AM, gschmidt notifications@github.com wrote:
|
Add optional else case to {{#with}}.
Sorry if I'm stepping on your toes, @belisarius222! I was looking for something simple to get started on. |
I'd like to respectfully suggest re-opening this issue and reverting the change. My reasoning for this includes the following:
Using an enclosing IF / ELSE block will be familiar to anyone who has used Handlebars outside of Meteor. One example of how the original behavior of the WITH block was beneficial is the case of merging Create & Update forms. With the original behavior, forms could be coded like this: <form>
{{#with person}}}
<input id="name" value="{{name}}" />
<input id="age" value="{{age}}" />
{{/with}}
</form> The form can be used for the Create case, where a person object does not already exist, as well as for the update case where a person does already exist. With the current behavior, the form contents do not display unless person exists, so duplication in a corresponding ELSE block is required. |
@alanning you could initialize the person object with defaults, maybe something like: return person || {}; |
In the example case, initializing with a default person object won't work because it changes the expected behavior. Extending the example to illustrate a more real-world usage, {{#with person}}}
<form>
<input id="name" value="{{name}}" />
<input id="age" value="{{age}}" />
{{#if person}}
<input type="submit" value="Update" />
{{else}}
<input type="submit" value="Create" />
{{/if}}
</form>
{{/with}} The workaround for the OP is to wrap the WITH block in an IF/ELSE. Reproducing the original behavior in the current code base can be achieved by overriding the WITH helper: Handlebars._default_helpers.with = function(context, options) {
return options.fn(context);
}; ...but this requires messing with internals, whereas reverting to the original behavior enables both scenarios using built-in functionality. I agree HB spec shouldn't necessarily dictate Meteor usage, but in this case I think the original behavior is more flexible. |
@alanning I see, |
@alanning @glasser I think we may have gotten distracted by a quick solution to a deeper issue. @allaning Based on your example, if
Note the addition of the |
{{#with falseyValue}} should render {{else}} content
I've been using Since
to
... which as far as my understanding goes, will only compute (Note that "notequals" isn't stock Meteor -- it's a made-up Handlebars helper used to help illustrate the example) |
{{#with falseyValue}} should render {{else}} content
Using the
#with
helper that resolves to a falsy value can lead to very strange results that are quite difficult to debug. I suggest Meteor do a check that makes sure that what is being passed to#with
is not falsy.An example to reproduce the strangeness:
The text was updated successfully, but these errors were encountered: