JSON Object Property Hierarchy Bug in Handlebars

I recently ran into a bug with the Handlebars client-side templating system. This issue was that Handlebars wasn't picking up a property on the JSON object I passed into it -- or, more specifically, it wasn't picking up the value while within an "unless" conditional block. It was picking up the value outside of the block.

The value in question was nested within another object of the object I passed in. After some digging, it turns out that, when within an "unless block", you need to add another ../ to the hierarchy.

To illustrate the fix, here is an example of some code that won't work. In the following example, we are 2 levels deep within the object's properties, and want to use the Id property that resides at the root level.

{{#unless IsDefault}}
{{../../Id}}
{{/unless}}

To get the code to work, add an additional "../" as shown below. While this doesn't accurately represent the object's structure, it does fix the problem. Kind of a hacky workaround, but barring a legitimate fix in Handlebars, it gets the job done.

{{#unless IsDefault}}
{{../../../Id}}
{{/unless}}

Comments

Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

"$.ajax is not a function" Error When Using jQuery and Darren Johnstone's ASP.NET Upload/Download Module v2