Handlebars – #startsWith helper

#startsWith helper renders the block if the test string begins with the specified prefix. If it does not match, the {{else}} block is rendered.

Usage

{{#startsWith prefix testString}}Block if match{{else}}Fallback{{/startsWith}}

prefix (required): the prefix to check for

testString (required): string to test against.

Examples

1.Check if a greeting starts with “Hello”

{{#startsWith "Hello" record.greeting}}Match!{{else}}No match{{/startsWith}}
{{#startsWith "Goodbye" record.greeting}}Wrong!{{else}}Still here{{/startsWith}}

Input:

{ "record": { "greeting": "Hello, world!" } }

Output:

Match!
Still here

2.Check if a customer name starts with “Jane”

{{#startsWith "Jane" record.firstName}}
  Welcome, Jane!
{{else}}
  User not Jane
{{/startsWith}}

Input:

{ "record": { "firstName": "Jane Doe" } }

Output:

Welcome, Jane!

Notes

  • Matching is case-sensitive (“Hello” ≠ “hello”).
  • Use when validating prefixes in IDs, codes, or user inputs.

Leave a comment

Your email address will not be published. Required fields are marked *