23
Views
1
Comments
[Handlebars.Net] Handlebars #If and #unless do not work
handlebars-net
Service icon
Forge asset by Miguel Antunes
Application Type
Traditional Web

Hi Miguel,

I am using your Handlebars.Net component, but I noticed that the #if and #unless conditions from Handlebars do not work.

Apparently, #if and #unless in Handlebars.NET do not work well with dynamic objects (see this post).

The solution apparently is to deserialize using Newtonsoft.Json.JsonConvert to an ExpandoObject.

I tried this out, and that indeed fixes the problem, and apparently should also be 10x more performant (didn't test that though).

I've included the updated extension (.xif) as an attachment to this post.

You can reproduce the problem (and check the fixed version) using your demo app with the following template and JSONData:

Template:


{{ #if isNewUser }}
You are a new user
{{ /if }}
{{ #unless isNewUser }}
You are an existing user
{{ /unless }}

JSON Data:

{
"isNewUser": true
}

Handlebars_Net.xif
2023-03-30 10-13-40
Miguel Antunes

Thank you, @Steven Decock for the provided solution. I've included it in the new component release. Together with your fix, I've upgraded Handlebars.Net package to version 2.1.2.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.