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

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Silent Renew and the "login_required" Error When Using oidc-client

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4