Skip to main content

Handlebars {{#with}} helper doesn't work for array elements, does it?

Answered

Comments

4 comments

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

    Hi Courtney,

    Nice to hear from you and thanks for the reply. I can confirm that your example compiles and works. It doesn't quite achieve what I was after so I tried to nest two {{#with}} statements to get into the context of the array element like so:

    {{#with this}}
    {{#with 0}}
    {{id}}
    {{/with}}
    {{/with}}

    No bueno - that doesn't work either. No error, but {{id}} evaluates to '' (empty string). I tried bracket notation and that DOES work:

    {{#with this}}
    {{#with [0]}}
    {{id}}
    {{/with}}
    {{/with}}

    So that's better than nothing. I'd still love it if handlebars could use

    {{#with this[0]}}

    Update: On a whim, I tried:

    {{#with this.[0]}}

    And it works! Eureka!

    Celigo should document this or have an example using it as I think it makes for much cleaner and more maintainable template code.

     

     

     

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

    Hi Steve Klett,

    Thanks for letting us know about this need and what you encountered. I'm checking with our experts on this and will get back to you as soon as possible.

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

    Hi Steve Klett,

    In the "with" statement, you don't set the index on the array object at first. Essentially, #with saves you from typing out the array object name for each element in the array you want to return. Using your example from above, please change it to the following:
    {{#with this}}
    <Id>{{0.id}}</Id>
    <sku>{{0.Name}}</sku>
    <description>{{0.Description}}</description>
    {{/with}}

    Let us know if that doesn't work!

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

    Hi Steve Klett, 

    Thank you so much for letting us know of this solution you found! We will definitely get this documented as soon as we can. We really appreciate you sharing this!

    0

Please sign in to leave a comment.