Skip to main content

Variables in CML

CML variables let you save and reuse values in calculations & conditions, simplifying logic, reducing data calls, and boosting efficiency.

Ryan Miller avatar
Written by Ryan Miller
Updated over a week ago

Overview

Variables in CML let you save a value within a calculation and then reuse that value later in a CML call. This not only simplifies the writing and management of CML Calculations or CML Conditions but also reduces data calls and improves processing times for CML that draws on multiple points of dynamic data or long strings of calculations.

Please use the table below as a guide for what CML tags are supported when utilising CML variables

CML

Description

[saveVar](insert name of variable)[/saveVar]

Tag used for saving a value as a variable

[useVar](insert name of variable)[/useVar]

Tag used for using a previously saved variable

How to save a CML variable

You can save a variable by adding the [saveVar] tag to your CML calculation.

Note: you can only save variables inside [calc] tags.

Example:

[calc]5#+#3[saveVar]total[/saveVar][/calc]

Here:

  • The calculation 5+3 is performed.

  • The result (8) is saved as a variable named total.

From now on, whenever you call the variable ‘total’, it will return the value 8.

Calling a CML Variable

To call a previously saved variable, use the [useVar] tag in your CML.

[useVar]insert variable name[/useVar]

This will display the variable to the end user.


CML Variables in Calculations

Variables in CML allow you to store a number or result from one calculation, then reuse it in another. This is especially useful when you want to reference the same value multiple times without having to repeat the entire formula.

Using a CML Variable in a Calculation

To reuse a variable, wrap its name in [useVar] tags and input it where desired on the page. This could be in another calculation, a condition, or even just referenced on its own.

Example:

[calc][useVar]total[/useVar]#x#2[/calc]

Here’s what happens:

  • [useVar]total[/useVar] is replaced by the saved value 8

  • The calculation becomes 8 x 2

  • The result returned is 16

Chaining Calculations with Variables

Variables are essential when you want to build calculations step by step.

Example:

[calc]20#-#7[saveVar]step1[/saveVar][/calc]

[calc][useVar]step1[/useVar]#+#3[saveVar]final[/saveVar][/calc]

[calc][useVar]final[/useVar]#x#2[/calc]

Here's what happens:

  1. 20 - 7 = 13 → saved as step1.

  2. step1 + 3 = 16 → saved as final.

  3. final x 2 = 32 → result shown.

This way, you can break complex calculations into smaller, reusable parts.

Tip: Use clear, descriptive names for your variables (height, weight, finalScore) so it’s easy to keep track of what each one represents. Variable names must be one word and must not include any spaces.

For more information on calculations in CML, please check out this article.


CML Variables in Conditions

Variables in CML allow you to use a stored value in a CML condition, enabling you to write simple, clear CML conditions. This is especially useful when you want to reference a previously calculated value in a condition.

Once a variable has been saved, you can reference it inside a [condition] by using the [useVar] tag.

Example:

[condition][useVar]score[/useVar]#>#10[result]You passed![/result][/condition]

Here’s what happens:

  1. [useVar]score[/useVar] is replaced with 15 (the value we saved earlier).

  2. The condition becomes 15 > 10.

  3. Since this is true, the text inside [result] is displayed: “You passed!”.

For more information on CML conditions, please check out this article.

Did this answer your question?