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 }}!
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:
| Form | Example |
|---|---|
| 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 }}!
{{= $upperName (uppercase name)}}
Welcome, {{ $upperName }}!
Context and output
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 }}
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 }}
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 }}
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}}"
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.