Skip to main content

FAQ: How can I substitute a default value when a handlebars expression for a field value evaluates to null?

Comments

2 comments

  • Rob Riccio

    What does this expression do if the first 31 char are not null?  Return the whole lastname field value, or the substring, or...?

    Trying to put this syntax in natural language, because this from the documentation didn't do it for me:

    If the argument in the {{#if field}} is true, then it prints the value from the context. If the argument is false (either undefined, null, " ", 0, or [ ] ), then the block prints the else condition. 

    {{#if lastname}}{{substring lastname "0" "30"}}{{else}}{{firstname}}{{/if}}

    Check the field {lastname}.  If the substring specified returns any valid value (Because there is no logical condition included such as =),  output value from {lastname}.  Else output value from field {firstname}.  ?

    Or: Output value from {lastname} IF the substring specified returns any valid value (Because there is no logical condition included such as =).  Else output value from field {firstname}.  ?

    Or: Check the field {lastname}.  If the substring specified returns any valid value (Because there is no logical condition included such as =),  output substring specified.  Else output value from field {firstname}.  ?

    Not sure what work putting lastname in the first brackets with #if is doing?  #2 seems most likely to me.  Is it specifying the input to the argument?  Or the output?  Or both?  There is no condition here which can be 'True' or 'False' in the logical sense, so it's tripping me up.  Something like the below would make sense to me as an argument, checking is the field lastname longer than 31 characters?  If it is, the expression would be F, if not, the values match and it would evaluate T.   

    {{#if lastname}} = {{substring lastname "0" "30"}}
    0
  • Tom Santiago Principal Technical Writer Community moderator
    Engaged
    Top Contributor
    Celigo University Level 4: Legendary

    Hi Rob Riccio,

    For clarification, this is a nested expression that uses two different handlebars expressions that can be considered independently: #if_else and substring.

    For #if_else, if the argument in the {{#if field}} is false (meaning it is undefined, null, " ", 0, or [ ]), then the block prints the else value. If the argument is true, the else condition is never triggered and the string prints as received from the source data.

    The secondary nested substring expression only limits the value returned by the "true" value of the #if_else to a maximum of 31 characters.

    So for this: 

    {{#if lastname}}{{else}}{{firstname}}{{/if}}

    If the record returns a value for the lastname field of any length, it's prints that value (otherwise it prints whatever returns in the firstname field). 

    Adding the substring expression: 

    {{#if lastname}}{{substring lastname "0" "30"}}{{else}}{{firstname}}{{/if}}

    The same behavior happens with the exception that the value will be limited to 31 characters at maximum.

    Only the first 31 characters of any returned lastname value will return. In both cases, the firstname prints only if the lastname value is undefined, null, " ", 0, or [ ].

    Is that less confusing?

    0

Please sign in to leave a comment.