# Variables

#### What are variables?

ESL allows you to create variables, which are like temporary merge fields, to store data for using multiple times in the content you're working on.

For example, you might need to use a [switch statement](https://resources.ubiquity.co.nz/home/using-engage/engage-scripting-language/directives#switch) to determine a value that needs to be output in an email for different contacts. If you were just outputting the value in one place, then placing the switch where you want the output to go would be simple.

But if you wanted to output that same value in two or more locations through the template, then you'd need to replicate your switch two or more times. Any changes to that switch would then be time consuming.

Instead this can be done with a variable - the switch could go at the top of the template which assigns the values to the variable. Then later in your email you can simply merge that variable out. This way there is only one switch statement to maintain if changes are needed.

***

### Creating a variable

Variables names always starts with an @ sign. To merge out the value of a variable it should be wrapped in square brackets.

```
{{ Variable(@varname as "type") }}
    [@varname]
{{ EndVariable }}
```

You can only merge a variable between the Variable and EndVariable tags.

Types available are:

* MobileNumber
* WholeNumber
* Decimal
* Date
* Time
* YesNo
* Text
* UniqueIdentifier

Type is optional. If you don't set a type UbiQuity defaults to "Text".

```
{{ Variable(@varname) }}
```

You can define more than one variable at a time, however they must all have the same type.

```
{{ Variable(@variable, @variable2 as "type") }}
```

**Examples**

Create a variable called Count, which is a WholeNumber, then merge that variable out. Note though that as we haven't assigned a value to the variable it will be null, and nothing will be output on the page.

```
{{ Variable(@count as "WholeNumber") }}
   [@varname]
{{ EndVariable }}
```

***

### Assigning a value to a variable

Variables aren't much use if you don't assign them a value. To assign a value to a variable you use Assign, and can use this in two different ways: either as a string or as a block.

**String assigns**

String assigns are useful when you want to just insert a simple value or line of text. They aren't ideal if your content contains quotes (as it can incorrectly close the ESL) and you can't use merge fields or track links inside the value. Note that UbiQuity will trim spaces on either side of your value with a string assign.

```
{{ Assign(@varname eq [Merge]/"Value") }}
```

**Block assigns**

Block assigns are useful when you want to include more complex content such as a block of HTML with merge fields, quotes and links.

```
{{ Assign(@varname) }}
    Some content with [@othervariables], [Merge Field]'s and <a href="http://www.ubiquity.co.nz">links</a>.
{{ EndAssign }}
```

**Examples**

Create a variable called count, and assign it a value of 1:

```
{{ Variable(@count as "WholeNumber") }}
    {{ Assign(@varname eq 1) }}
    [@varname]
{{ EndVariable }}
```

Create a variable called intro and assign it some text, including a merge field:

```
{{ Variable(@intro) }}
    {{ Assign(@intro) }}
        Hey [Database.First Name] did you know the circus will be in your area next week?
    {{ EndAssign }}
    [@intro]
{{ EndVariable }}
```

This example includes a switch to swap out a store's name and contact phone number in an email depending on where each contact lives. If the contact doesn't match one of the other cases it will fall back to the Auckland store details. The name and phone number can be merged anywhere throughout the email so long as they're between the Variable and EndVariable tags.

```
{{ Variable(@storename, @storephonenumber) }}
    {{ Switch([Database.City]) }}
        {{ Case("AKL") }}
            {{ Assign(@storename eq "Auckland") }}
            {{ Assign(@storephonenumber eq "09 123 4567") }}
        {{ EndCase }}
        {{ Case("WLG") }}
            {{ Assign(@storename eq "Wellington") }}
            {{ Assign(@storephonenumber eq "04 234 5678") }}
        {{ EndCase }}
        {{ Case("CHC") }}
            {{ Assign(@storename eq "Christchurch") }}
            {{ Assign(@storephonenumber eq "03 345 6789") }}
        {{ EndCase }}
        {{ Default }}
            {{ Assign(@storename eq "Auckland") }}
            {{ Assign(@storephonenumber eq "09 123 4567") }}
        {{ EndDefault }}
    {{ EndSwitch }}
    
    <p>Hi [Database.First Name]</p>
    <p>To get in contact with our [@storename] store call [@storephonenumber] during normal business hours.</p>
    <p>Cheers,</p>
    <p>The team at [@storename] store</p>

{{ EndVariable }}
```

***

### Increment/decrement a variable

If your variable is a WholeNumber type, then you can increment or decrement that number.

This is often used to keep count of something, such as how many rows you have in a table.

```
{{ Increment(@varname, [Merge]/[@othervariable]/amount) }}
{{ Decrement(@varname, [Merge]/[@othervariable]/amount) }}
```

The amount is optional, if you don't specify this then your variable will be incremented by 1.

```
{{ Increment(@varname) }}
{{ Decrement(@varname) }}
```

**Example**

This example creates a variable called Count and assigns it to 1, then increments it by 2. The resulting output on the page would be "1, 3":

```
{{ Variable(@count as "WholeNumber") }}
    {{ Assign(@count eq 1) }}
    [@count], 
    {{ Increment(@count, 2) }}
    [@count]
{{ EndVariable }
```

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ubiquity.co.nz/documentation/keep-learning/esl-ubiquitys-scripting-language/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
