Skip to content

A false result of a boolean helper is interpreted as a truthy "false" string #361

@sferencik

Description

@sferencik

A 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions