Skip to content

Blocks

Block tags wrap a body of nested segments and control whether — or how many times — that body renders. Every block form pairs an open tag ({{#if …}}) with a matching close tag ({{/if}}).

{{#if}} / {{else if}} / {{else}}

{{#if user.isAdmin}}
  Welcome, admin.
{{else if user.isMember}}
  Welcome, member.
{{else}}
  Please sign in.
{{/if}}

Arms are tried top to bottom; the first arm whose expression is truthy renders, and the rest are skipped. {{else}} — with no expression — always matches if reached. With no matching arm and no {{else}}, the block renders nothing.

{{#unless}} / {{else}}

{{#unless user.isBanned}}
  Welcome back.
{{else}}
  Access denied.
{{/unless}}

The inverse of {{#if}}: the body renders when the expression is falsy; {{else}} renders when it's truthy. {{#unless}} supports only a single optional {{else}} — no else if chain.

{{#each}} / {{else}}

{{#each items}}
  - {{ this }} (#{{ @index }})
{{else}}
  No items.
{{/each}}

Iterates a list or a struct pushed onto the context stack one element at a time (see @index/@key for reading the current position). {{else}} renders when the collection is empty or the expression resolves to neither a list nor a struct.

Context type Iterates Pushes
List elements in list order @index (0-based)
Struct entries in insertion order @key
anything else (including absent) nothing — {{else}} runs

{{#with}}

{{#with user.address}}
  {{ street }}, {{ city }}
{{/with}}

Pushes the expression's result as the new current context for the body, without iterating — useful for narrowing into a nested object without repeating its path on every line. {{#with}} has no {{else}} arm.

Nesting and ../

Every block push (an {{#each}} iteration or a {{#with}}) adds one frame to the context stack. Use ../ inside a nested block to reach back out to an enclosing frame — see Interpolation for the full walk-up rule, which applies equally to plain identifiers and to @index/@key.

Whitespace trimming

Block tags accept the same ~ trimming markers as interpolation tags — {{#each items ~}}, {{~ /each}}, {{#if ~cond~}}, and so on. See Template Basics.

Where to next

  • Expressions — the truthiness rule every block condition uses, and the operators available inside {{# … }}.
  • Interpolation@index/@key and ../ in full.
  • Partials{{> key }}, which composes with {{#each}} to render one partial per loop iteration.