0

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.

2
  • 1
    Your template look OK to me. Could there be an issue with your #equals helper? Commented Jun 17, 2023 at 12:09
  • I edited the question and added the equals helper. @76484 Commented Jun 17, 2023 at 13:47

1 Answer 1

0

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);
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.