Q. The {{#contains}} handlebars block helper evaluates to true if a string is found in a variable. It also allows an {{else}} condition to show a different value if the test fails.
But what I’m trying to accomplish is to test for different strings, along the lines of if-else if or switch-case logic. For example, my API’s IDs have separate prefixes for order types: “SVC-” for services, “PRD-” for products, and so on. Is there a way in a handlebars statement to test for both strings?
A. As long as your substrings are certain to be unique, you can apply multiple {{#contains}} blocks. Only the expressions that succeed will be mapped.
In the example below, a source id field of “SVC-1” would cause the current destination field to be hard-coded to “Services.” The second condition would be ignored.
{{#contains id "SVC-"}}Services{{/contains}}{{#contains id "PRD-"}}Products{{/contains}}