Handlebars-Net/Handlebars.Net

Nested object with same name in parent object

Closed this issue · 1 comments

Was just randomly testing some expressions and ran into one. Ran it through official handlebars and it rendered as expected.

In a #each expression, when iterating through objects with nested objects, if the nested object has a property with same name as parent object property the parent object property is shown.

			var data = new
			{
				test = new
				{
					complexItems = new[] {
						new { name = "a", value = 1, evenMoreComplex = new { name = "zzz", abbr = "z" } },
						new { name = "b", value = 2, evenMoreComplex = new { name = "yyy", abbr = "y" } },
						new { name = "c", value = 3, evenMoreComplex = new { name = "xxx", abbr = "x" } }
					},
				}
			};
				{{#with test}}

					{{#if complexItems}}
					<ul>
						{{#each complexItems}}
							<li>{{name}} {{value}} {{evenMoreComplex.name}} {{evenMoreComplex.abbr}}</li>
						{{/each}}
					</ul>
					{{/if}}

				{{/with}}

expected

					<ul>
							<li>a 1 zzz z</li>
							<li>b 2 yyy y</li>
							<li>c 3 xxx x</li>
					</ul>

actual

					<ul>
							<li>a 1 a z</li>
							<li>b 2 b y</li>
							<li>c 3 c x</li>
					</ul>

Seems you can work around it using with for that object...

				{{#with test}}

					{{#if complexItems}}
					<ul>
						{{#each complexItems}}
							<li>{{name}} {{value}} {{#with evenMoreComplex}}{{name}} {{abbr}}{{/with}}</li>
						{{/each}}
					</ul>
					{{/if}}

				{{/with}}