Working with variables

In addition to the predefined variables, you can specify your own hashes and variables.  In some cases, you do this by declaring your own variables, and in other cases it happens as the implicit result of directives such as <#list> and <#data>.

Defining variables in the template

In addition to using variables defined in the data model, you can define variables in the template for use only in that template. These temporary variables can be created and replaced using RPL directives. Note that each template processing job has its own private set of these variables that exist while the given page is being rendered. This variable set is initially empty, and will be discarded when the template processing job is finished.

You access variables defined in the template in the same way as variables in the data model.

Template variables take precedence over data model variables with the same name. This means that if a template variable and a data model variable have the same name, the value of the template variable will be used. Note that the value of the data model variable will not be overwritten. If you want to use the data model variable instead of a template variable, use the .globals variable. For example, assume the variable user in the data model has the value Big Joe:

<#assign user = "Joe Hider">
${user}          <#-- prints: Joe Hider -->
${.globals.user} <#-- prints: Big Joe -->  

You can define the following variables in a template:

Variable type

Description

Global

Accessible from everywhere in the template, and from the templates inserted with include directive. You can create and replace these variables with the assign or macro directives.

Local

Can be set only inside a macro definition body, and are only visible from there. A local variable exists only for the duration of a macro call. You can create and replace local variables inside macro definition bodies with the local directive.

Macro parameters are local variables.

Loop

Created automatically by directives such as list and data. These variables exist only between the start and end tags of the directive.

The following example creates and replaces variables with the assign directive:

<#assign x = 1>  <#-- create variable x -->
${x}
<#assign x = x + 3> <#-- replace variable x -->
${x}  

produces this output:

1

Local variables overwrite global variables with the same name. Loop variables overwrite local and global variables with the same name. For example:

<#assign x = "plain">
1. ${x}  <#-- we see the plain var. here -->
<@test/>
6. ${x}  <#-- the value of plain var. was not changed -->
<#list ["loop"] as x>
    7. ${x}  <#-- now the loop var. hides the plain var. -->
    <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->
    8. ${x}  <#-- it still hides the plain var. -->
</#list>
9. ${x}  <#-- the new value of plain var. -->
 
<#macro test>
  2. ${x}  <#-- we still see the plain var. here -->
  <#local x = "local">
  3. ${x}  <#-- now the local var. hides it -->
  <#list ["loop"] as x>
    4. ${x}  <#-- now the loop var. hides the local var. -->
  </#list>
  5. ${x}  <#-- now we see the local var. again -->
</#macro>  

produces this output:

1. plain
  2. plain
  3. local
    4. loop
  5. local
6. plain
    7. loop
    8. loop
9. plain2

An inner loop variable overwrites an outer loop variable:

<#list ["loop 1"] as x>
  ${x}
  <#list ["loop 2"] as x>
    ${x}
    <#list ["loop 3"] as x>
      ${x}
    </#list>
    ${x}
  </#list>
  ${x}
</#list>  

produces this output:

  loop 1
    loop 2
      loop 3
    loop 2
  loop 1  

Note that the value of a loop variable is set by the directive invocation that created it (list in the example). This is the only way to change the value of a loop variable. However, as shown in the above example, you can temporarily hide a loop variable with another loop variable.

About predefined variables

Predefined variables are defined by the system, and are usually initialized from the campaign definition and other internal values. Some of the more useful ones are campaign.name and campaign.marketingprogram. For a complete list of predefined variables, see Namespaces.

Next steps

Namespaces

Retrieving variables

Handling missing variables

Learn more

Namespace Reference

Directive Reference

Templates