Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine
MIT License
1.26k stars 217 forks source link

Very weird behavior inside of a partial with an #each after passing in a custom context to that partial #432

Closed HandleBarsNetQuestion closed 3 years ago

HandleBarsNetQuestion commented 3 years ago

In the below code, the value of actual1 is "," and the value of actual2 is ",False,I shouldn't show up!,"

I expected the partial "displayList" to have a new context, which would be null since "TheList" is never assigned. I expected the #each to not iterate through anything.

Test to reproduce

        private class ClassWithAList
        {
            public IEnumerable<string> TheList { get; set; }
        }

        private class ClassWithAListAndOtherMembers
        {
            public IEnumerable<string> TheList { get; set; }
            public bool SomeBool { get; set; }
            public string SomeString { get; set; } = "I shouldn't show up!";
        }

        [Fact]
        public void WeirdBehaviour()
        {
            var handlebars = Handlebars.Create();
            handlebars.RegisterTemplate("displayListItem", "{{this}},");
            handlebars.RegisterTemplate("displayList", "{{#each this}}{{> displayListItem}}{{/each}}");

            var template = handlebars.Compile("{{> displayList TheList}}");
            var actual1 = template(new ClassWithAList());
            var actual2 = template(new ClassWithAListAndOtherMembers());
            var expected = "";

            Assert.Equal(expected, actual1);
            Assert.Equal(expected, actual2);
        }