Handlebars.Net icon indicating copy to clipboard operation
Handlebars.Net copied to clipboard

Support "else X" for block helpers

Open pmccloghrylaing opened this issue 6 years ago • 6 comments

In the JS library you can chain else with any block helper - not just if and unless.

I'm currently using else with a custom helper that does Regex matching:

{{#match cultureCode "^de\b"}}
...
{{else match cultureCode "^it\b"}}
...

This example can be worked around easily by changing the helper and switching to sub-expressions ({{#if (match ...)}}), but this is something that works in the JS library.

It would be nice if it allowed if, each and unless to be defined as regular helpers.

I created a solution for this (before I realised else if was supported) that expanded {{#A}}{{else B}}{{/A}} into {{#A}}{{else}}{{#B}}{{/B}}{{/A}}. Looking at the existing code it seems like combining the ConditionalBlockAccumulatorContext and BlockHelperAccumulatorContext would be a better approach.

pmccloghrylaing avatar Aug 28 '18 05:08 pmccloghrylaing

The readme seems to showcase this as doable. What am I missing? See here:

{{StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}

sferencik avatar May 28 '20 10:05 sferencik

@sferencik Readme misses # before block helper call. Valid example:

{{#each this}}
   The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n
{{/each}}

oformaniuk avatar Jun 13 '20 02:06 oformaniuk

So you're saying it's an issue with the documentation (readme) but the feature exists?

sferencik avatar Jun 16 '20 12:06 sferencik

So you're saying it's an issue with the documentation (readme) but the feature exists?

It depends what feature you’re referring to. If you’re talking about behavior from Readme and your example - yes, it’s implemented. It you’re talking about the subject of the Github issue - no, it’s not implemented yet.

oformaniuk avatar Jun 16 '20 15:06 oformaniuk

Oh, I get it. This is about a kind-of "else if" behaviour: {{else CONDITION}}.

sferencik avatar Jun 23 '20 10:06 sferencik

#358 fixes the readme.

sferencik avatar Jun 23 '20 10:06 sferencik