Skip to content

Parent context in an ifCond in a each is wrong #539

Closed

Description

@hifi-phil

When using parent context in a ifCond in an each then data scope is different to what happens in handlebars js

The scope of ../this should be the root where as ../../this points to the root

Test to reproduce

[Fact]
public void with_an_ifCond_in_a_loop_then_the_parent_this_context_in_the_ifCond_should_be_the_root()
{
    var source = @"{{#each loop}}
                        {{#ifCond another '===' 'value'}}
                            test {{{../this.foo}}}
                        {{/ifCond}}
                    {{/each}}";
    var partialSource = "test {{foo}}";
    var template = HandlebarsDotNet.Handlebars.Compile(source);

    using (var reader = new StringReader(partialSource))
    {
        var partialTemplate = HandlebarsDotNet.Handlebars.Compile(reader);
        HandlebarsDotNet.Handlebars.RegisterTemplate("partial", partialTemplate);
    }

    HandlebarsDotNet.Handlebars.RegisterHelper("ifCond", (writer, options, context, parameters) =>
    {
        if (parameters.Length != 3)
            options.Inverse(writer, context);
        else
        {
            if (parameters[0] == parameters[2])
                options.Template(writer, context);
            else
                options.Inverse(writer, context);
        }
    });

    var data = new { foo = "bar", loop = new object[] { new { another = "value" } } };

    var output = template(data);
    Assert.Equal(output, "test bar");
}

Has anyone else had this issue? I'm happy to work on a pull request but don't know where to start

Activity

  1. oformaniuk commented on Feb 14, 2023

    @oformaniuk
    Member

    Hi @hifi-phil
    My best guess would be to start from checking if path is parsed and handled correctly, have a look at PathInfo.Parse

  2. added 2 commits that reference 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