Skip to main content

Variables

A template variable stores a value that can be reused later in the same render. Variables are prefixed with $ and assigned with the = mustache:

{{= $name "John"}}
Hello, {{ $name }}!
Context and output

Context

{}

Generated output

Hello, John!

The assignment mustache produces no output of its own. It is "standalone" in the comment sense — when alone on a line, the line is removed.

Assignment forms

The right-hand side of {{= $name <value>}} can be any expression that is also valid as a helper argument:

FormExample
Literal{{= $count 5}}, {{= $greeting "hi"}}, {{= $on true}}
Path{{= $userName user.name}}
Variable{{= $copy $original}}
Helper call{{= $upper (uppercase name)}}

Examples:

{{= $userName user.name}}
Hello, {{ $userName }}!
Context and output

Context

{
"user": {
"name": "Alice"
}
}

Generated output

Hello, Alice!
{{= $upperName (uppercase name)}}
Welcome, {{ $upperName }}!
Context and output

(With an uppercase helper registered.)

Context

{
"name": "john"
}

Generated output

Welcome, JOHN!

Reassignment

A variable can be reassigned. The new value replaces the old one immediately; subsequent reads see the new value:

{{= $x 1}}
{{= $y 2}}
{{= $sum (add $x $y)}}
{{= $result (multiply $sum 10)}}
Result: {{ $result }}
Context and output

Context

{}

Generated output

Result: 30

Scoping: variables are global

Unlike block-pushed context, variables are not scoped to the block they were assigned in. An assignment inside a block is visible after the block closes, in sibling blocks, and in parent blocks:

{{= $var "global"}}
{{#condition}}
{{= $var "block"}}
{{/condition}}
Outside: {{ $var }}
Details

Context

{
"condition": true
}

Generated output

Outside: "block"

This is intentional — variables are how you accumulate state across iterations. The classic use is a running total in a loop:

{{= $sum 0}}
{{#numbers}}
{{= $sum (add $sum $this)}}
{{/numbers}}
Total: {{ $sum }}
Context and output

Context

{
"numbers": [1, 2, 3, 4, 5]
}

Generated output

Total: 15

If you need block-local state, use a fresh variable name per block.

Undefined reads

Reading a variable that was never assigned does not error; it resolves to undefined and renders as the empty string:

Value: "{{$undefined}}"
Context and output

Context

{}

Generated output

Value: ""

Variables versus context

Variables and context fields live in different namespaces. {{name}} reads from the context; {{$name}} reads from the variable map. A variable assignment never modifies the context, and a context value is never visible as a variable. Use whichever is appropriate; if you find yourself mirroring context into variables, you probably want to read from context directly.