Handlebars

A logic-light templating language, superset of Mustache.

Keeps templates clean by pushing logic into helpers rather than inline expressions.

Syntax basics

{{variable}}                      Output, HTML-escaped
{{{variable}}}                    Raw HTML output (unescaped)
{{! comment }}                    Comment (not rendered)
{{> partialName}}                 Include a partial
{{#> layout}}content{{/layout}}   Layout/wrapping partial
  • Path expressions are dot-notation only.

  • No bracket access: property names with hyphens or starting with digits need a custom helper or pre-processing.

  • Whitespace: add ~ to strip whitespace around blocks ({{~#each~}}), otherwise indentation leaks into the output.

Built-in blocks

#if / #else / #unless

  • Falsy values: empty array/string, false, 0, null, undefined.
  • Empty objects {} are truthy.
  • #if is falsy-based, not existence-based: false and 0 fail the check even if the key exists. Use a custom helper if you need "is this key present regardless of value."

#each

iterates arrays (or objects, giving keys):

{{#each items}}
  {{@index}}: {{this.name}}
{{/each}}

Special vars: @index, @key, @first, @last, this.

#with

shifts context scope:

{{#with user.profile}}
  Name: {{name}}, Bio: {{bio}}
{{/with}}

Pitfalls

  • XSS: {{var}} auto-escapes, {{{var}}} does not — use raw output only with trusted content.
  • Prototype pollution: New Handlebars versions has guards, keep it updated. Never compile untrusted user templates server-side.