#### Helper for local variables

We use it to record the results of calling other helpers and use them further inside the current template.

Let's look at its work with two examples.

## Example 1

Let's say a user ordered a mango, a watermelon and a melon. The quantity and prices of all ordered things were included in the variables in this form:

{ "mango Price": 300, "mango Count": 5, "watermelon Price": 20, "watermelon Count": 30, "melonPrice": 50, "melon Count": 8 }

The **quantity** of mangoes ordered was recorded in **mangoCount** variable, the **price** — in **mangoPrice**.

The **weight** of a watermelon in kg was recorded in **watermelonCount** variable, the **price** of a watermelon per kg — in **watermelonPrice**.

The **weight** of a melon in kg was recorded in **melonCount** variable, the **price** of a melon per kg — in **melonPrice**.

We use **multiply** helper to multiply the quantity of the product by its price:

{multiply mangoCount mangoPrice}}

Let's do it for all products:

{{multiply watermelonCount watermelonPrice}} {{multiply melonCount melonPrice}}

In order not to do these calculations every time, we use var helper to write them to local variables, and then use them in several places where we need them.

For the mango, we will write the calculations in **mangoTotal**.

For the watermelon — in **watermelonTotal**.

For the melon — in **melonTotal**.

All together will look like this:

{{#vars}} {{var "mangoTotal" (multiply mangoCount mangoPrice)}} {{var "watermelonTotal" (multiply watermelonCount watermelonPrice)}} {{var "melonTotal" (multiply melonCount melonPrice)}}

Now, using helper **{{#vars}}** with the data written into it, you can do calculations and not prescribe the same data every time:

Mango: {{mango Count}}pcs. {{@vars.manga Total}} $ Watermelon: {{watermelon Count}}kg. {{@vars.watermelon Total}} $ Melon: {{melon Count}}kg. {{@vars.melonTotal}} $

This is especially convenient if there are a lot of calculations.

Now let's calculate the total amount of ordered goods using **var** helper, with the recorded data.

{{add (add @vars.mangoTotal@vars.watermelonTotal) @vars.melonTotal}} {{/vars}}

You can give variables any names and write any calculations in them.

You can test this example in the sandbox →

## Example 2

Let's say the user ordered a mango, a watermelon and a melon again. But the quantity and prices of everything ordered were included in the variables in the form of a list, that is slightly different:

{ { "products": [ { "id": "1024", "title": "Mango", "count": 5, "price": 300 }, { "id": "2048", "title": "Watermelon", "count": 30, "price": 20 }, { "id": "2048", "title": "Melon", "count": 8, "price": 50 } ] }

Let's take a step-by-step look at all the steps for calculations.

You can use variables only inside block **{{#vars}}**.

1. Using helper var, write to tolal variable zero, because not a single product has been recorded there yet:

{{var "total" 0}}

2. Sort through the list of products using helper **each**:

{{#each products}}

Using **each**, go inside the list so you can work with variables as with ordinary variables that are not inside the list.

Now you have:

**title**— the product name;**count**— the quantity;**price**— the price per unit.

Inside **each**, we multiply the variable with the price by the variable with the quantity:

{{multiply count price}}

Let's write this into new variable **subtotal**, which we will create using var:

{{var "subtotal" (multiply count price)}}

Now in **subtotal** we have the result of multiplying the price by the quantity.

3. Output a numbered list of products with the subtotal.

A numbered list can be made with **@index**, a special variable in which the current number of the list item is written.

Since the lists are numbered from zero, we add **add @index 1 **unit to it.

And we output a previously recorded variable in which we already have the result of multiplying the price by the quantity:

{{@vars.subtotal}} {{add @index 1}}. {{title}} — {{price}} ₽ / {{count}} pcs. — {{@vars.subtotal}} ₽

Where:

**{{add @index 1}}**— will be output as the order sequence number. In this case it is 1,2,3;**{{title}}**— will be output as the product name. In our example, this is mango, watermelon, melon;**{{price}}**— the price of 1 piece in one position;**{{count}}**— the quantity of the ordered product;**{{@vars.subtotal}} ₽**— the price for the ordered quantity of one item of goods (the quantity multiplied by the price).

**Each** helper is executed for each entry in the list, so the whole list will be output as a result. In our case, the result will be three ordered products:

- Mango — 300 ₽ / 5 pcs. — 1500 ₽
- Watermelon — 20 ₽ / 30 pcs. — 600 ₽
- Melon — 50 ₽ / 8 pcs. — 400 ₽

4. Now add up the subtotal with the total. To do this, overwrite **total** variable.

The subtotal is recorded as many times as there are items in the list. Each time we write the subtota to the same variable — **tolal**.

With each new item in the list, that is, with a new product, a new amount is overwritten in **total**, but already taking into account the new product. There are three items in our list, so the subtotal is overwritten three times.

Before **each** closing, the total amount of purchases was recorded in **total**, which can be output with **{{@vars.total}}**. In our case, it is 2500 ₽.

Total: {{@vars.total}} ₽ → Total: 2500 ₽

Don't forget to close **{{/vars}}**.