Skip to main content

Handlebars helper reference

Comments

51 comments

  • Harish Bakka
    Engaged

    Thanks, Stephen, it worked. will post if I have any issues.

     
    0
  • Steve Klett Channel Partner
    Awesome Follow-up
    Engaged
    Celigo University Level 2: Seasoned

    I have a use case where I need to define a JSON array by nested enumeration of hierarchical source data. The challenge is how to avoid the trailing comma for the last element.

    [
    3,
    6, <-- Err!
    ]

    Because the source data is hierarchical, I can't make the comma conditional on the outer #each and likewise I can't make it conditional on the nested, inner #each; I need to consider BOTH of them when determining if a comma should be created or not.

    Were it not for the inability to access the @last variable in the parent scope, something like this would work perfect:

    [
    {{#each outer_array}}
    {
    "name": "foo {{{name}}}"
    },{{#each inner_array}}{
    "size": {{{size}}}
    }{{#unless @last && ../@last}},{{/unless}}
    {{/each}}
    {{/each}}
    ]

    The key is checking for the end of the outer and inner loops.

    {{#unless @last && ../@last}},{{/unless}}

    The problem is, ../@last doesn't work. I can't find any way to access @ variables from the inner loop scope.


    UPDATED 6/17/2021 –
    Solved, self-answered:

    The @ symbol precedes the path: 

    {{{@../index}}}
    • Celigo action item: update @last with example for nested loops
    0
  • Courtney Jordan Experience Strategy & Design Director Community moderator
    Celigo University Level 4: Legendary
    Awesome Follow-up
    Top Contributor

    Hi Dominik Antalík,

    Thanks for letting us know about your use case. I apologize for the late response - I missed this comment coming in. Have you gotten this resolved yet? I'm checking with our handlebars experts.

    0
  • Courtney Jordan Experience Strategy & Design Director Community moderator
    Celigo University Level 4: Legendary
    Awesome Follow-up
    Top Contributor

    Hi Dominik Antalík,

    We do not support registering custom helpers at this time. If you could enter an enhancement request, we can gauge whether to add this to the roadmap. Thanks!

    0
  • A Arora

    HI,

    If we have an array of objects:

    [{amount: 10,...}, {amount:20,...}, {amount:20,...}]

    How can we add all amount field from each object then multiply by 100 using handlebar expressions?

    0
  • Stephen Brandt Documentation Manager Community moderator
    Engaged
    Great Answer
    Top Contributor
    Celigo University Level 3: Master

    Hi, A Arora. Thanks for contacting us, and sorry for the delay over the long U.S. holiday weekend. 

    A few suggestions are posted in a related Community thread. The easiest, according to the in-house experts would be to transform the data structure to flatten the amounts into a single array. Then, you can call sum as follows:

    {{multiply (sum amount)  100}}

    You should consider the other solutions in the linked post if you needed to maintain your present record structure for downstream processing. 

    0
  • Steve Klett Channel Partner
    Awesome Follow-up
    Engaged
    Celigo University Level 2: Seasoned

    timestamp should be updated to indicate that when no format is supplied it will return an ISO8601 string (what I wanted). I had to go dig through moment.js docs to determine that for sure.

    0
  • Steve Klett Channel Partner
    Awesome Follow-up
    Engaged
    Celigo University Level 2: Seasoned

    On the dateFormat documentation for i/pformat:

    This field is the date object referenced in the Context.

    Whaaaaa? ;)

     

    0
  • Tom Santiago Senior Technical Writer Community moderator
    Engaged
    Top Contributor
    Celigo University Level 4: Legendary

    Steve Klett 

    Thanks for this information regarding the default timestamp format. I've revised the article to reflect your research. 

    For the dateFormat question, we use the term "Context" to refer to the source data from the incoming record to be interpreted by the handlebars expression, so the following statement:

    "This field is the date object referenced in the Context."

    a few lines down from that we state:

    "Input format (i/pformat) is where you define the "input" format contained in the Context file"

    So if you have an incoming record with a specific format, you would specify the incoming format as the i/pformat. You would then use the o/pformat to define the format that you want to convert the incoming format into. 

    Does that make more sense? Please email me at tom.santiago@celigo.com if you'd like to further discuss how I can improve the documentation so that it's less confusing.

    0
  • Steve Klett Channel Partner
    Awesome Follow-up
    Engaged
    Celigo University Level 2: Seasoned

    Hi,

    The regexMatch documentation doesn't make any sense to me. The property description for regex is wrong:

    regex: the new value which will replace the existing value.

    The first two samples seem to have the syntax for regexReplace, not to mention the second one is maybe not a great example to replace a value with the same value. Anyway, I had to spend quite a bit of time looking at this trying to figure out if I'm dense and need a break of if the docs are totally confusing me.

    0
  • Josyrley de Almeida

    Hi,
    I have this to get the current date
                     {{dateFormat "YYYY-MM-DD HH:mm" date}}
    I'd like to add the timezone (Europe/London).

    I tried this {{dateFormat "YYYY-MM-DD HH:mm" date "YYYY-MM-DD[T]hh:mm:ss.sss[Z]" "Europe/London"}} but I got the error

    "Could not compile handle bar \{{dateFormat "YYYY-MM-DD HH:mm" date "YYYY-MM-DD[T]hh:mm:ss.sss[Z]" "Europe/London"}}\" because \"handlebars Helper {{dateFormat}} expects date argument when you have i/p date format\" .Please correct and retry"

    How can I add the timezone when getting the current date?

    Thank you.
    Josy

    0
  • Kate Larson Principal Technical Writer Community manager
    Celigo University Level 2: Seasoned

    Hi Josyrley de Almeida -
    I checked in with one of our handlebars gurus and this was his response:
    If you don't specify an input date format (it's often not required), you can use a blank string for it when adding timezone. In a handlebar w/out timezone, the blank string is assumed.

    {{dateFormat "YYYY-MM-DD HH:mm" date "" "Europe/London"}}

    In order for

    {{dateFormat "YYYY-MM-DD HH:mm" date "YYYY-MM-DD[T]hh:mm:ss.sss[Z]" "Europe/London"}} 

    to work, there would need to be a source field "date" in YYYY-MM-DD[T]hh:mm:ss.sss[Z]" format. Also, I don't think brackets around the [T] and [Z] are correct, even if there is that field.

    YYYY-MM-DDTHH:mm:ssZ

    If you need any further assistance, please sign up for an office hours session. Thanks!

    0
  • Stephen Lemp

    What is the proper syntax for the {{else_if}} block?

    0
  • Tom Santiago Senior Technical Writer Community moderator
    Engaged
    Top Contributor
    Celigo University Level 4: Legendary

    Hi Stephen Lemp,

    Here is an example of  {{else if}}:

    {{#if name}}{{name}}{{else if test}}Steve{{else}}Kat{{/if}}

    In the above example, if the name field exists, the value of name prints.

    If the name field does not exist {{else if}}, and a field named test exists, then Steve prints.

    If neither name nor test fields exist, Kat prints.

    We'll revise this section of the article for clarity soon.

    0
  • Brian Brown

    Any recommendations on how to dateAdd to get the previous month?  1 month previous to today that is.

    All we really care about is 'MON-YYYY' format.

    Thanks!

    0
  • Dan Claypool NetSuite Integration Whiz Strategic Support Lead
    Celigo University Level 4: Legendary

    Hi Brian,

    Assuming that your input date is something like "2021-12", the following should work:

    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jan"}}{{dateFormat "[Dec]-YYYY" (dateAdd [Order Date] "-31556952000")}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Feb"}}{{dateFormat "[Jan]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Mar"}}{{dateFormat "[Feb]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Apr"}}{{dateFormat "[Mar]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "May"}}{{dateFormat "[Apr]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jun"}}{{dateFormat "[May]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jul"}}{{dateFormat "[Jun]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Aug"}}{{dateFormat "[Jul]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Sep"}}{{dateFormat "[Aug]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Oct"}}{{dateFormat "[Sep]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Nov"}}{{dateFormat "[Oct]-YYYY" [Order Date]}}{{else}}{{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Dec"}}{{dateFormat "[Nov]-YYYY" [Order Date]}}{{else}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}

    If your input date is in a different format, you'll just need to change "YYYY-MM" (the input dateFormat) to the proper format.

    Additionally, the above assumes that the field is named "Order Date" in your source data.

    Here's a version of the above code block that should be a bit easier to read, but you'll want to use the one above in your integration:

    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jan"}}
    {{dateFormat "[Dec]-YYYY" (dateAdd [Order Date] "-31556952000")}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Feb"}}
    {{dateFormat "[Jan]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Mar"}}
    {{dateFormat "[Feb]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Apr"}}
    {{dateFormat "[Mar]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "May"}}
    {{dateFormat "[Apr]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jun"}}
    {{dateFormat "[May]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Jul"}}
    {{dateFormat "[Jun]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Aug"}}
    {{dateFormat "[Jul]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Sep"}}
    {{dateFormat "[Aug]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Oct"}}
    {{dateFormat "[Sep]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Nov"}}
    {{dateFormat "[Oct]-YYYY" [Order Date]}}
    {{else}}
    {{#contains (dateFormat "YYYY-MMM" [Order Date] "YYYY-MM") "Dec"}}
    {{dateFormat "[Nov]-YYYY" [Order Date]}}
    {{else}}
    {{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}{{/contains}}}

    Dan

    0
  • Brian Brown

    Thanks Dan Claypool.  Looks like that formula is exceeding the maximum byte size of the FILE NAME field though.  We're looking to use this handlebar expression to provide the file name in an FTP operation.

    No way to actually addMonth ?  

    Moment.js does offer a way to addMonth.  And from what I understand Celigo uses moment.js on the backend.

    No way to make an addMonth call in handlebars at all huh?

    Thanks for your input on this!

     

    0
  • Dan Claypool NetSuite Integration Whiz Strategic Support Lead
    Celigo University Level 4: Legendary

    Hi Brian,

    Ah, I didn't realize this was for an FTP filename.

    We do not have a handlebar helper for addMonth, however, please feel free to file an enhancement request here.

    An alternative would be the following:

    {{dateFormat "MMM-YYYY" (dateAdd [Order Date] "-2592000000")}}

    The above can be inaccurate, as it subtracts 2592000000 milliseconds (30 days) from the given timestamp. For instance, if the "Order Date" is 2021-03-01, the output would be "Jan-2021, since February only has 28 (or 29) days.

    Another alternative would be if you can include the date with the previous month in your export data, you can reference this field in your filename.

    Best Regards,
    Dan

    0
  • Dan Claypool NetSuite Integration Whiz Strategic Support Lead
    Celigo University Level 4: Legendary

    Hi Brian,

    I had a little more time to clean up that first handlebar helper and created one that uses the current timestamp then changes that value to the prior month (and year, for dates in January). This should do the trick:

    {{#contains (dateFormat "MMM") "Jan"}}{{dateFormat "[Dec]-YYYY" (dateAdd "-31556952000")}}{{else contains (dateFormat "MMM") "Feb"}}{{dateFormat "[Jan]-YYYY"}}{{else contains (dateFormat "MMM") "Mar"}}{{dateFormat "[Feb]-YYYY"}}{{else contains (dateFormat "MMM") "Apr"}}{{dateFormat "[Mar]-YYYY"}}{{else contains (dateFormat "MMM") "May"}}{{dateFormat "[Apr]-YYYY"}}{{else contains (dateFormat "MMM") "Jun"}}{{dateFormat "[May]-YYYY"}}{{else contains (dateFormat "MMM") "Jul"}}{{dateFormat "[Jun]-YYYY"}}{{else contains (dateFormat "MMM") "Aug"}}{{dateFormat "[Jul]-YYYY"}}{{else contains (dateFormat "MMM") "Sep"}}{{dateFormat "[Aug]-YYYY"}}{{else contains (dateFormat "MMM") "Oct"}}{{dateFormat "[Sep]-YYYY"}}{{else contains (dateFormat "MMM") "Nov"}}{{dateFormat "[Oct]-YYYY"}}{{else contains (dateFormat "MMM") "Dec"}}{{dateFormat "[Nov]-YYYY"}}{{/contains}}

    Here it is in action:


    Dan

    0
  • Dan Claypool NetSuite Integration Whiz Strategic Support Lead
    Celigo University Level 4: Legendary

    Hi Brian,

    One of our engineers, Lucian Hymer, came up with a more elegant solution:

    {{#compare (dateFormat "MM") "==" 1}}Dec-{{subtract (dateFormat "YYYY") 1}}{{else}}{{dateFormat "MMM" (subtract (dateFormat "MM") "1") "MM"}}-{{dateFormat "YYYY"}}{{/compare}}

    This will give the same output as well:

    Dan

    0
  • Brian Brown

    Hey Dan, awesome!  This is great and is working wonderfully!  Really appreciate this quick response and great solution.  Thanks Dan!

    0

Please sign in to leave a comment.