Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parent context in an ifCond in a each is wrong

hifi-phil opened this issue · comments

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

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