Skip to content

ifs are not evaluated if there are invisible characters #605

Closed

Description

@panmona

Describe the bug

In case there are invisible characters (in this case it is a BOM) in an if statement, Handlebars compiles the template without any errors. But the if is not evaluated.

Expected behavior:

I expect that the if would be evaluated.

Test to reproduce

    [Fact]
    public void Compiles_Ifs_Correctly()
    {
        string source =
            @"<div class=""entry"">
<h1>{{title}}</h1>
<div class=""body"">
{{body}}
{{#if someCondition\uFEFF}}
<p>Show additional text</p>
{{/if}}
</div>
</div>";

        var handlebars = Handlebars.Create();
        var template = handlebars .Compile(source);
        var data = new {
            title = "My new post",
            body = "This is my first post!",
            someCondition = true
        };

        var actual = template(data);
        string expected = 
            @"<div class=""entry"">
<h1>My new post</h1>
<div class=""body"">
This is my first post!
<p>Show additional text</p>
</div>
</div>";
        Assert.Equal(expected, actual);
    }

Other related info

This happens to us as we allow admin users to manually edit the templates and sometimes they manage to insert invisible characters like byte order marks into the template.


After submitting the issue

Please consider contributing to the project by submitting a PR with a fix to the issue.
This would help to solve your problem in a shorter time as well as help other users of the project.
-> Pointers for contributing this fix would be appreciated.

Activity

  1. added a commit that references this issue on Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions