A false result of a boolean helper is interpreted as a truthy "false" string
See original GitHub issueA false result of a boolean helper is interpreted as a truthy "False"
string.
Test case:
{{#if (Regex.IsMatch "a" "b")}}
TRUE
{{else}}
FALSE
{{/if}}
This prints TRUE; we should expect FALSE.
(Regex.IsMatch
taken from https://github.com/StefH/Handlebars.Net.Helpers/wiki/Regex#ismatch .)
IsMatch
correctly evaluates the regex match as (bool) False
and prints it to the text writer. Then the sub-expression handler pulls it out as the "False"
string and hands it over to the built-in “#if”. The “#if” expects a boolean value so it runs HandlebarsUtils.IsTruthyOrNonEmpty("False")
which evaluates it as true
.
I believe "False"
should evaluate to false
, especially if produced by a boolean
expression. The user shouldn’t be affected by the fact that Handlebars.Net converts the value to string first (and pulls it out using CaptureTextWriterOutputFromHelper()
).
For comparison, this works fine in JS:
handlebars.registerHelper('soBoolean', () => false);
const boolTemplate = handlebars.compile('{{#if (soBoolean)}}TRUE{{else}}FALSE{{/if}}');
console.log(boolTemplate()); // TRUE
The easy fix is to change IsFalsy()
to perform Boolean.TryParse()
for string values.
We might also want to consider my earlier suggestion that helpers should return value rather than print it to the text writer.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
OK.
Once you are complete, create an issue in https://github.com/StefH/Handlebars.Net.Helpers project so I can take action to upgrade.
Great news, @zjklee @rexm!
On Thu, Sep 24, 2020 at 6:04 PM Oleh Formaniuk notifications@github.com wrote: