Handlebars supports two comment styles. A comment never appears in the rendered output, and the exclamation point (!) is what marks an expression as a comment. Choose the comment style carefully: the inline form ends at the first }}, so a comment that contains }} or a mustache expression must use the block form instead.
Comment styles
| Style | Syntax | When to use |
|---|---|---|
| Inline comment |
{{! comment }}
|
Short comments that don't contain }} or other Handlebars expressions. |
| Block comment |
{{!-- comment --}}
|
Comments that contain }}, mustache expressions such as {{name}}, or that span multiple lines. |
A standard expression renders its value:
{{expression}}
Adding ! turns it into a comment, so it doesn't render:
{{!expression}}
Comment out an expression
Adding ! to an expression comments it out so it produces no output. In this example, the address, city, state, and zip expressions are commented out, so the template renders only each item's name:
{{#each custom_fields}}{{name}}{{!address}}{{!city}}{{!state}}{{!zip}}{{/each}}
Examples
An inline comment doesn't appear in the output:
{{! This comment will not show up in the output }}
A block comment also doesn't appear in the output, and can safely contain }} and mustache expressions:
{{!-- This comment will not show up in the output --}}
Avoid the closing-brace pitfall
An inline comment ({{! }}) ends at the first }}. If the comment contains }} or a mustache expression such as {{comment}}, the parser treats everything after that first }} as template code, which can cause a compilation error.
For example, this inline comment breaks, because the parser ends the comment at the braces inside it and then tries to evaluate the rest:
{{! --This {{comment}} may result in the compiler evaluating everything after the second set of braces }} and thus throwing a compilation error.
To safely include }} or a mustache expression in a comment, use the block form:
{{!-- This {{comment}} will not show up in the output --}}