Local (template-scoped) variables


Using the setVar and getVar helpers you can create template-scoped (or local) variables to save some intermediate data in your templates and avoid repeating the same logic multiple times.

 Local variables scope

Unlike global variables which are shared between all routes of an environment, local variables are scoped to the template they are used in. Their values are regenerated each time the template is re-evaluated.

 Local variables support

Local variables are available everywhere templating helpers are supported.

 Usage

To set and access local variables, two templating helpers are available.

 Set a local variable

To set a variable, use the setVar helper. This helper takes two arguments: the variable name and its value. You can dynamically set the parameters using other helpers.

Some examples:

Copy
{{setVar 'varname' 'value'}} {{setVar 'varname' (body 'id')}}

 Get a local variable

To get a global variable, use the getVar helper or refer to the variable with its name prefixed with an @. The helper takes two arguments: the variable name and an optional path. You can dynamically set the parameters using other helpers and use the fetched data in other helpers.

Some examples:

Copy
# Set a variable {{setVar 'varname' 'value'}} # Get a variable {{@varname}} {{getVar 'varname'}} {{getVar (body 'property')}}