local
Synopsis
or
or
Where:
- name: the name of the local object in the root. It is not an expression. However, it can be written as a string literal, which is useful if the variable name contains reserved characters, for example
<#local "foo-bar" = 1>
. Note that this string literal does not expand interpolations (as"${foo}"
). =
: Assignment operator, which can also be one of the shorthand assignment operators (++
,+=
, etc.), just like with theassign
directive,- value: the value to store. Expression.
Description
It is similar to assign
directive, but it creates or replaces local variables. This only works inside macro definitions and function definitions.
For more information about variables, read this: Defining variables in the template
A frequent mistake is trying to use
assign
to change a local variable like:<#macro m><#local x = 1>${x}<#assign x = 2>${x}</#macro>
. This prints11
, not12
, becauseassign
creates/replaces thex
of the namespace that the template belongs to, and doesn’t change thex
local variable. Local variables should be always set with thelocal
directive, not just for the fist time.