Handling missing variables

The data model often has variables that are optional (i.e., sometimes missing). RPL does not support referring to missing variables unless you explicitly specify what to do if a variable is missing.

This section describes the two most common ways of handling missing variables.

NOTE: RPL treats both non-existent variables and a variables with null values as missing.

Specifying a default value

When referring to a variable that might be missing, you can specify a default value. To do this, follow the variable name with an exclamation mark (!) and the default value.

In the following example, when user is missing, “Anonymous” will be used as the value. When user is not missing, the default value is ignored:

<h1>Welcome ${profile.user!"Anonymous"}!</h1>  

Please note that missing field names at the root namespace will result in empty strings, and they will appear to have a value. For instance:

<h1>Welcome ${FIRRST_NAME!”Unknown”}!</h1>

(Notice the double ‘R’)

Will never result in Unknown since the variable is assumed to have a value.

Specifying a default value for sub-variables

To specify a default value for a sub-variable, follow the sub-variable name with a ! as shown in the following example:

<h1>Welcome ${user.firstname!"Anonymous"}!</h1>  

Note that the above example is correct if only firstname is missing but user is not. If user is missing as well, an error will occur that terminates template processing. To avoid such errors, enclose both the top-level variable and the sub-variable in parenthesis:

<h1>Welcome ${(user.firstname)!"Anonymous"}!</h1>  

Testing for missing values

To find out whether a value is missing, follow the variable name with double question marks (??).

As shown in the following example, you can combine ?? with the if directive to skip the greeting if the user variable is missing a value:

<#if profile.user??><h1>Welcome ${user}!</h1></#if>  

Please note that in the prior examples, the use of profile.user will be missing only if they were not aliased on the datasources in the campaign design. If they were aliased, the result is that the variable is present. If want to check whether a given value was a null or empry string, or a null in the case of numbers and dates, you can use the ?isnull builtin (explained in detail later.) Briefly, ?isnull is usually used in if directives. The following is an example, with the erroneous FIRRST_NAME of previous examples:

<h1>Welcome <#if FIRRST_NAME?isnull>Unknown<#else>${FIRRST_NAME}</#if>!</h1>

The builtin ?isnull is usually applied towards empty strings, and to numbers and dates coming from the personalization record. You cannot create null dates or numbers in RPL other than from the personalization record.

Important: Regarding variable accessing with multiple steps, such as animals.python.price, writing animals.python.price!0 is correct only if animals.python is never missing and only the last sub-variable, price, is possibly missing (in which case here we assume it's 0). If animals or python is missing, the template processing will stop with an "undefined variable" error. To prevent this, you have to write (animals.python.price)!0. In that case the expression will be 0 even if animals or python is missing. The same logic applies to ??; animals.python.price?? versus (animals.python.price)??.

Testing for missing sub-variables

To test whether a value is missing in a sub-variable, follow the sub-variable name with double question marks (??). For example:

<#if user.firstname??><h1>Welcome ${user}!</h1></#if>  

Note that the above example is correct if only the value of firstname is missing, but not of user. If user is missing as well, an error will occur that terminates template processing. To avoid such errors, enclose both the top-level variable and the sub-variable in parenthesis:

<#if (user.firstname)??><h1>Welcome ${user}!</h1></#if>  

Next steps

Expressions

Working with variables

Retrieving variables

Learn more

Handling missing values