Skip to content

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

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

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