Skip to main content

Handlebars helper reference

Follow

Comments

32 comments

  • Tony Zuko

    Thank you for the reference! Where can I find a list of options for the "options" parameter for regexMatch?

    Comment actions Permalink
  • Tom Santiago

    Hi Tony, I've updated the article to link to this resource for regex options: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions. I hope that helps. Thanks for the feedback.

    Comment actions Permalink
  • Jeff Dollard

    Is there an example for the #switch syntax?  It's the only block logical operator that doesn't have any hint as to the syntax.

     

    Comment actions Permalink
  • Courtney Jordan

    Hi Jeff Dollard,

    Thanks for providing this feedback! We're looking into this for you.

    Comment actions Permalink
  • David Gollom

    The substring description for the end index doesn't match the behavior.  The end index acts like javascript and it's non-inclusive.  That should be noted above.  This was baffling me for a bit.  How did this {{substring "itemizationCode" 0 4}} return "item" ?? 

     

    Comment actions Permalink
  • Courtney Jordan

    Hi David Gollom,

    Thanks for letting us know - I updated the article with this note. Is there anything else we should add based on your findings? Thank you so much for your help!

    Comment actions Permalink
  • Courtney Jordan

    Hi Jeff Dollard,

    We haven't found any information on the switch handlebars. Have you had any luck?

    Comment actions Permalink
  • Jeff Dollard

    Hi Courtney - no luck with the switch syntax, I sort of gave up on guessing at it ¯\_(ツ)_/¯

     

    I found another way to structure my logic, so it didn't keep me from moving forward.

    Comment actions Permalink
  • Courtney Jordan

    Awesome, glad to hear it, Jeff Dollard! Any cool tips you can share that we could add to the docs?

    Comment actions Permalink
  • APC DSR

    Shouldn't the output be "123 Anywhere789 Somewhere"? Both have values.

    Comment actions Permalink
  • Tom Santiago

    Hi APC DSR, 

    This image describes why the expression evaluates to false:

    We've revised the example for clarity.

    Comment actions Permalink
  • Courtney Jordan

    Thank you so much for letting us know how we could improve the article, APC DSR! Please let us know if there are other things we can add! We've also added more examples to the #and section. Hope they're helpful!

    Comment actions Permalink
  • viliandy leonardo

    Jeff Dollard 

    I want to let you know that the "Switch" syntax is not supported. We are updating this KB. Thanks for bringing it to our attention!

    Comment actions Permalink
  • Jeff Dollard

    Do sum and add helpers only work with integers or can they be used with decimals/float values?

    Comment actions Permalink
  • Courtney Jordan

    Hi Jeff Dollard,

    That's a great question and we'll update our article accordingly. I just tested this and decimals and floats seem to work fine for both add and sum. Let us know if you run into any issues!

    Comment actions Permalink
  • Steve Klett

    I'm trying, but I don't understand the purpose of getValue,- why not just reference the field directly?

    {{field}}

    Apparently there is a second parameter with "default value" used in the example, but I don't how it's used.

    Why does the following example return null? I would expect either an empty string or... "default Value"

    {{getValue "legends.total" "defaultValue"}}

    Anyway, I was reading this page (again) and stopped on getValue to learn about it and after failing to do so I thought I'd leave this comment.

     

     

    Comment actions Permalink
  • David Gollom

    The regexReplace section seems to be missing something in the template.  It should read {{regexReplace field replacement regex options}}. I'm not sure about the index bit.  There's reference to an index parameter but I wasn't able to get that to work.   Thanks! 

    Comment actions Permalink
  • Stephen Brandt

    Hi, Steve Klett. That's a great question. After conferring a bit, we realized that this description indeed needs clarification, and there are some scenarios where {{getValue}} is your "helper" after all. 

    getValue comes in handy when the name of a field is dynamically generated. For example, consider this sample data:

    {
    "date": "2021-01-09",
    "2021-01-09": "3"
    }

    Let's say that we need to retrieve the value for 2021-01-09, and that field name was dynamically generated in the data.

    We would not be aware of that name ahead of time. Instead, we can identify that field by looking at the value for the date field, as follows:

    {{getValue (getValue "date")}}

    Since "getValue" is a helper method, we can nest other helper methods inside it, which also opens up multiple options for dynamically generating the field name.

    Comment actions Permalink
  • Steve Klett

    Not sure what level of feedback is actually wanted by the Celigo team, this may be considered petty - if so, tell me offline.

    The subtract documentation states:

    Note: This helper does not require quotation marks for JSON variables.

    This is inaccurate, you can use Number types rather than strings.

    {{subtract 10 6}} == 4

    The description also shows the syntax as

    {{subtract field field}}

    seems like

    {{subtract value value}}

    would be more general.

    Comment actions Permalink
  • Stephen Brandt

    David Gollom, your post is consistent with the examples and every regexReplace usage/result that I've seen. 

    I suspect that that was a copy-paste error from {{substring}}, waiting many months for your careful read. We'll review and update. Thanks much!

     

    Comment actions Permalink
  • Courtney Jordan

    Hi Steve Klett,

    Never would we turn down your valuable help! We really appreciate you sharing this with us. I've made updates to the subtract section. Please let us know if you notice anything else we should change. Thank you so much for your help!

    Comment actions Permalink
  • Stephen Brandt

    Thanks, Steve Klett, for the non-petty feedback. This is the right place, for other readers, as well. 

    Your uses of {{subtract}} are correct, and the examples in the article would benefit from demonstrating additional data types, which we'll add. 

    A couple points of clarification: 

    • I suspect that this helper has become more "forgiving" with allowed values since this article was written.
    • The note about "quotation marks for JSON variables" is confusing and should be removed. However, I don't think that it was added to make any distinction between passing strings vs. integers. It was probably to avoid having new customers type {{subtract "product.qty" "sold"}} as opposed to {{subtract product.qty sold}}.
    Comment actions Permalink
  • Stephen Lemp

    The behavior of @last with objects seems to be the negative of @first. I.e. An object with 3 keys will return true for @last for the 2nd and 3rd key. Is that expected?

    Comment actions Permalink
  • Stephen Lemp

    Hi Stephen, I missed an important note in the documentation: "The first and last steps of iteration are noted via the @first and @last variables when iterating over an array. When iterating over an object only the @first is available."

    The behavior I was seeing was when iterating over an object.

    Comment actions Permalink
  • Courtney Jordan

    Hi Stephen Lemp,

    Thanks for calling our attention to this area for potential confusion. We've added an example and added information on the zero-indexing of arrays to the @first, @last sections. Please let us know if there are other ways we could improve this documentation! 

    Comment actions Permalink
  • Stephen Brandt

    Thanks for the clarification, Stephen Lemp. I was able to reproduce that problem with this simple object, and indeed @last evaluated to true on every inner loop after the first. 

    {
    "tickets": [
    {
    "charge": "40 mph in a 30 mph zone",
    "fine": "$100",
    "disposition": "guilty"
    }
    ]
    }

    Poking around at handlebars resources online, it seems that iterating through nested objects is not roundly and fully supported. Assuming that you're trying to check @last in order to properly format the JSON in a template, I appealed to a support lead here, who came up with a clever solution: add the comma before printing the array based on the value of @first instead. 

    Your use case is doubtless more complex, but let me know if that's not what you need or if we should look into the problem further. 

    Comment actions Permalink
  • Stephen Lemp

    Stephen Brandt Thank you so much - that's exactly what I needed! I had resorted to getting the number of keys (number_of_keys) with JavaScript and printing the comma if @index != number_of_keys. Your solution is much cleaner and I can get rid of the convoluted JavaScript logic :D. Thanks again! The use case here is actually taking an object and creating a SQL INSERT INTO statement. SQL is picky about the commas, just like JSON though, so essentially the same.

    Comment actions Permalink
  • Dominik Antalík

    Hello Celigo,

    is there a way how to register new custom Handlebars helper?

    I've came to the handlebars engine limitation when I cannot use if helper inside multiply helper.

    I've found workaround for that with custom helper, however I do not know how to register it.

     

    My case:

    when I want to do sum of tax rates but only when tax amount is greater than zero... For instance, you can have four different tax rates in US, however for shipping only two-three of them are applied, but in the payload all four are available so I want to filter out rates with zero amount.

    origin handlebar without conditions:

    {{#if shipping_lines_1.price}}
    {{multiply 
    (sum shipping_lines_1.tax_lines.[0].rate 
    shipping_lines_1.tax_lines.[1].rate 
    shipping_lines_1.tax_lines.[2].rate 
    shipping_lines_1.tax_lines.[3].rate)
    100}}
    {{/if}}

     

    part of the payload regarding "shipping_lines":

    {"shipping_lines_1":
    {"id": 1111111111,"carrier_identifier": null,"code": "Standard Shipping","delivery_category": null,"discounted_price": "8.90","discounted_price_set": {"shop_money": {"amount": "8.90","currency_code": "USD"},"presentment_money": {"amount": "8.90","currency_code": "USD"}},"phone": null,"price": "8.90","price_set": {"shop_money": {"amount": "8.90","currency_code": "USD"},"presentment_money": {"amount": "8.90","currency_code": "USD"}},"requested_fulfillment_service_id": null,"source": "shopify","title": "Standard Shipping",
        "tax_lines": [
        {"price": 0.09,
        "price_set": {"shop_money": {"amount": "0.09","currency_code": "USD"},"presentment_money": {"amount": "0.09","currency_code": "USD"}},
        "rate": 0.0625,
        "title": "IL STATE TAX"},
        {"price": 0.00,
        "price_set": {"shop_money": {"amount": "0.00","currency_code": "USD"},"presentment_money": {"amount": "0.00","currency_code": "USD"}},
        "rate": 0,
        "title": "IL COUNTY TAX"},
        {"price": 0.00,
        "price_set": {"shop_money": {"amount": "0.00","currency_code": "USD"},"presentment_money": {"amount": "0.00","currency_code": "USD"}},
        "rate": 0.01,
        "title": "IL CITY TAX"},
        {"price": 0.07,
        "price_set": {"shop_money": {"amount": 0.07,"currency_code": "USD"},"presentment_money": {"amount": "0.07","currency_code": "USD"}},
        "rate": 0.0075,
        "title": "IL SPECIAL TAX"}],
        "discount_allocations": []
        }}

     

     

    as you can see the tax code with a name "IL CITY TAX" has rate 0.01 however amount is zero, which leads into miscalculation in the accounting system when imported shipping tax rate is 8% instead of 7%

    So I've added conditions to check tax amount/price at the first place and then do sum of rates when tax amount is greater than zero:

    {{#if shipping_lines_1.price}}
        {{multiply 
        (sum
        ({{#if shipping_lines_1.tax_lines.[0].price}}{{shipping_lines_1.tax_lines.[0].rate}}{{else}}0{{/if}})
         ({{#if shipping_lines_1.tax_lines.[1].price}}{{shipping_lines_1.tax_lines.[1].rate}}{{else}}0{{/if}})
         ({{#if shipping_lines_1.tax_lines.[2].price}}{{shipping_lines_1.tax_lines.[2].rate}}{{else}}0{{/if}})
         ({{#if shipping_lines_1.tax_lines.[3].price}}{{shipping_lines_1.tax_lines.[3].rate}}{{else}}0{{/if}})
        )100
        }}
        {{else}}0{{/if}}
    

    Unfortunately, I always get an error: " Could not compile handle bar ... Parse error on line 4 ... Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'OPEN_BLOCK'"

    So I've created custom helper which is easy to use on the page tryhandlebarsjs.com or jsfiddle.com, but not able to use in celigo mapping:

    Handlebars.registerHelper("ifThenElse", function (condition, ifTrue, ifFalse) {
        return condition ? ifTrue : ifFalse;
    });
    

    You would then use the helper in your template as a Handlebars Subexpression, which allows you to nest helpers:

    {{#if shipping_lines_1.price}}
      {{multiply 
        (sum 
          (ifThenElse shipping_lines_1.tax_lines.[0].price shipping_lines_1.tax_lines.[0].rate 0)
          (ifThenElse shipping_lines_1.tax_lines.[1].price shipping_lines_1.tax_lines.[1].rate 0)
          (ifThenElse shipping_lines_1.tax_lines.[2].price shipping_lines_1.tax_lines.[2].rate 0)
          (ifThenElse shipping_lines_1.tax_lines.[3].price shipping_lines_1.tax_lines.[3].rate 0)
        )
        100
      }}
    {{/if}}
    Comment actions Permalink
  • Harish Bakka

    How can I offset one day back to the current date.

    I want to have a date like if the current date is 05/06/2021 then it should be with offset '-1' 05/05/2021.

    Thanks

    Comment actions Permalink
  • Stephen Brandt

    Hi, Harish Bakka. The answer is above, if you scroll down to the offsetfield argument description for dateAdd

    Let me know if that example (in milliseconds) doesn't work for you:

    {{dateAdd dateOrders "-86400000"}}
    Comment actions Permalink

Please sign in to leave a comment.