handlebars.jshandlebarshelperhandlebars.net

Difficulties printing values with Handlebars #equals in Handlebars.net


I am trying to print a tag depending on the language and the date type, so when it DateAndTime the tag should be "Delivery date and time:" and when the type is Date it should be "Delivery date".

{{#equals locale "en"}}                                                                                
<span>                                                                                                     
<b>                                                                                                    
 <span data-contrast="none" xml:lang="EN-US" lang="EN-US" id="iemmm" 
 class="TextRun SCXW95494680 BCX0">
                                                                                                     
 <span class="NormalTextRun SCXW95494680 BCX0">
                                                                                                   
 {{#equals order.transport.DateType "DateAndTime"}}
                                                                                                       
 Delivery date and time:
                                                                                                   
 {{/equals}}
                                                                                                   
 {{#equals order.transport.DateType "Date"}} 
                                                                                                    
 Delivery date: 
                                                                                                     
 {{/equals}}
                                                                                                      
 </span>
                                                                                                      
 </span>
                                                                                                      
 </b>
                                                                                                  
 <span>{{order.transport.Date}}
                                                                                               
 </span>
                                                                                          
 </span>
{{/equals}}

and the content i send to handlebars is:

"locale" : "en",
"order" : {
 "transport": {
      "Date": "Saturday17June202316:46",
      "DateType": "DateAndTime",
      "Address": {
        "Name": "DimalCollaku",
        "CountryCode": "BE",
        "ZipCode": "9860",
        "City": "Oosterzele",
        "Street": "LangeAmbachtstraat",
        "Remarks": "Test",
        "Location": {
          "Latitude": 50.949455,
          "Longitude": 3.8165402
        }
      }
}

This is the 'equals' helper function:

    Handlebars.RegisterHelper("equals", (writer, options, context, arguments) =>
        {
            if (arguments.Length > 2)
            {
                throw new HandlebarsException("#equals helper requires exactly two arguments.");
            }

            if (arguments.Length != 2)
                options.Inverse(writer, context);

            if (arguments[0].Equals(arguments[1]))
                options.Template(writer, context);
            else
                options.Inverse(writer, context);
        });

but this way it doesn't print any tag only the value of {{order.transport.Date}}. When i try printing {{order.transport.DateType}} it prints correct DateAndTime.


Solution

  • The issue was in the equals helper defined in the code. The arguments[0].Equals(arguments[1]) comparison was performed using the Equals method, which does a reference comparison for strings. After updating the helper function the issue was fixed.

      Handlebars.RegisterHelper("equals", (writer, options, context, arguments) =>
        {
            if (arguments.Length > 2)
            {
                throw new HandlebarsException("#equals helper requires exactly two arguments.");
            }
    
            if (arguments.Length != 2)
                options.Inverse(writer, context);
            var arg1 = arguments[0].ToString();
            var arg2 = arguments[1].ToString();
    
            if (arg1 == arg2)
            {
                options.Template(writer, context);
            }
            else
            {
                options.Inverse(writer, context);
            }
        });