String Helper

Helper Description
slice Extract a portion of a string.
nltobr Replace \n with <br> tags.
sprintf Returns a string produced according to the formatting string format.
It uses sprintf-js internally.
Check https://github.com/alexei/sprintf.js for more information.
lowercase Changes the string to lowercase.
uppercase Changes the string to uppercase.
concat Concat two or more strings.
join Join the elements of an array with the specified separator.
split Split a string into an array by the specified characters.

slice

Extract a portion of a string.
Parameters:
  • val: string
    Target string.
  • start: number
    The zero-based index at which to begin extraction.
  • end: number
    The zero-based index before which to end extraction. The character at this index will not be included.
          // results in: Just
hbs.compile("{{slice str 0 4}}")({"str":"Just Wow"});

// results in: Wow
hbs.compile("{{slice str 5}}")({"str":"Just Wow"});

// results in:  Wow
hbs.compile("{{slice str -4}}")({"str":"Just Wow"});
        

nltobr

Replace \n with <br> tags.
Parameters:
  • val: string
    Target string.
          // results in: It's<br>just<br>now
hbs.compile("{{{nltobr str}}}")({"str":"It's\njust\nnow"});
        

sprintf

Returns a string produced according to the formatting string format.
It uses sprintf-js internally.
Check https://github.com/alexei/sprintf.js for more information.
Parameters:
  • format: string
    Format string.
  • args: ...any
    Any number of parameters/values.
          // Argument swapping:
// results in: Hello Dolly!
hbs.compile("{{sprintf '%s %s!' str1 str2}}")({"str1":"Hello","str2":"Dolly"});

// results in: foo bar 55 baz 20
hbs.compile("{{sprintf '%s %s %d %s %d' 'foo' 'bar' 55 'baz' '20'}}")();

// Named arguments:
// results in: Hello Dolly
hbs.compile("{{sprintf 'Hello %(name)s' user}}")({"user":{"name":"Dolly"}});

// results in: Hello Dolly
hbs.compile("{{sprintf 'Hello %(name)s' name=str}}")({"str":"Dolly"});
        

lowercase

Changes the string to lowercase.
Parameters:
  • val: string
    Target string.
          // results in: just wow
hbs.compile("{{lowercase str}}")({"str":"JUST WOW"});
        

uppercase

Changes the string to uppercase.
Parameters:
  • val: string
    Target string.
          // results in: JUST WOW
hbs.compile("{{uppercase str}}")({"str":"just wow"});
        

concat

Concat two or more strings.
Parameters:
  • args: ...string
    Any number of string arguments.
          // results in: Hello world!
hbs.compile("{{concat 'Hello' ' world' '!'}}")();
        

join

Join the elements of an array with the specified separator.
Parameters:
  • collection: string[]
    Array.
  • separator: string
    A string to separate each pair of adjacent elements of the array. If omitted, the array elements are separated with a comma (",").
          // results in: Hands & legs & feet
hbs.compile("{{{join collection ' & '}}}")({"collection":["Hands","legs","feet"]});
        

split

Split a string into an array by the specified characters.
Parameters:
  • val: string
    The value to split for.
  • separator: string
    Separator. Default is a comma (",").
          // results in: ['a', 'b', 'c']
hbs.compile("{{split list}}")({"list":"a,b,c"});

// results in: <ul>
//           <li>a</li>
//           <li>b</li>
//           <li>c</li>
//         </ul>
hbs.compile(`<ul>
              {{#each (split list ',')}}
                <li>{{this}}</li>
              {{/each}}
            </ul>`)({"list":"a,b,c"});