Helper {{#vars}}

You need this helper to create and use local variables in templates.

In fact, variables in templates are primarily for convenience, and their use is limited to your imagination, and not just the examples below.

To create variables in templates, you need to open the {{#vars}} block and declare the variable as the {{var}} string helper:

{{#vars}}
{{var "variableName" "Hello World"~}} 
{{/vars}}

In this example, variableName is the name of the variable that we want to create, and Hello World is the value that we write there.

When writing the lowercase version of var, due to the peculiarities of Handlebars, extra spaces and line breaks may appear in the texts. To avoid this, you need to put the tilde symbol ~: {{var "name" "value" ~}} in front of the closing columns. This is a standard part of Handlebars syntax which has no relation to this helper, but var is most often found with extra spaces and line hyphenations, that’s why we decided to add this information here.

After a local variable was declared inside the {{#vars}} block you can use it with the @vars object, this is a special object, that is vailable only inside the {{#vars}} block:

{{#vars}}
{{var "variableName" "Hello World"~}}
{{@vars.variableName}}
{{/vars}}

In this template, we created a variable named variableName and wrote the Hello World string into it, after that we typed this variable, i.e. we output the value of the Hello World string. Now it may seem useless, so let's look at another example.

Not only values known in advance, but also the results of other lowercase helpers can be written to variables. For example, we can cut a sentence into words and write it in a list using the split helper:

{{#vars}}
{{var "sentence" "And there in the woods and by the creek
and reeds and lilies of the valley were sprouting."~}}
{{var "words" (split @vars.sentence " ")~}}
{{stringify @vars.words}}
{{/vars}}

In this example, we wrote a sentence into the sentence variable, and then we wrote the result of the {{split @vars.sentence " "}} helper into the words local variable - cut the sentence into separate parts separated by a space. After recording, we typed the contents of the @vars.words variable as JSON by the stringify helper.

Inside the {{#vars}} block, you can use any other helpers, even another {{#vars}} helper.

With the help of variables, we can calculate how often the conjunction "and" occurs in a sentence:

{{#vars}}
{{var "sentence" "And there in the woods and by the creek
and reeds and lilies of the valley were sprouting."~}}
{{var "words" (split @vars.sentence " ")~}} {{var "countAnd" 0~}}
{{#each @vars.words~}}
  {{#eq (lowercase this) "and"~}}
    {{var "countAnd" (add @vars.countAnd 1)~}}
  {{/eq}}
{{/each}}
Total conjunctions «and»: {{@vars.countAnd}}
{{/vars}}

In this example, we create a variable with a sentence, a variable with words from a sentence. After that, we create the countAnd local variable and write the number 0 to it.

With the help of the {{#each}} helper, we iterate through the list inside the words local variable.

If the current element of the list - this - is equal to the "and" string, we compare it with the {{#eq}} helper, then we write the sum of the past values of countAnd and 1 to the countAnd local variable, i.e. we simply increase the sum by one.

As the "and" conjunction can be written with both a large and a small letter, we applied the {{lowercase}} helper to this, which makes all the letters in the string lowercase, i.e. removes the uppercase ones.

In the end, we type the string: A total of the "and" conjunction : adding to it the value from the countAnd local variable.

Local variables can also be used to process user variables.

Let's say the user has a list of products stored in the products variable in the same form, possibly Request received by the component or created by the Write Variable component:

[
  { "name": "Melon", "cost": 300, "count": 1 },
  { "name": "Orange", "cost": 80, "count": 15 },
  { "name": "Apple", "cost": 30, "count": 32 }
]

We can output it, and calculate the total amount using variables:

{{#vars}}
{{var "totalPrice" 0 ~}}
{{var "totalCount" 0 ~}}
{{#each  products}}
{{var "subtotalPrice" (multiply this.count this.cost) ~}}
{{var "totalPrice" (add @vars.totalPrice @vars.subtotalPrice) ~}}
{{var "totalCount" (add @vars.totalCount this.count) ~}}
{{this.name}} — {{this.cost}} $/pieces — {{this.count}} pieces — {{@vars.subtotalPrice}} $
{{/each}}
Total: {{@vars.totalPrice}} $ for
{{@vars.totalCount}} pieces
{{/vars}}

If we execute the same pattern, we will get the same result.

Melon — 300 $/pieces. — 1 pieces. — 300 $
Orange — 80 $/pieces. — 15 pieces. — 1200 $
Apple — 30 $/pieces. — 32 pieces. — 960 $
Total: 2460 $ for 48 pieces

Let's analyze the example in parts.

{{var "totalPrice" 0 ~}}
{{var "totalCount" 0 ~}}

Here we create two local variables, TotalPrice and totalCount, for the sum of total goods and the sum of their quantity, respectively, and write number 0 in them. After creation, we can read these variables using a special @vars: @vars.totalPrice and @vars.totalCount objects respectively.

{{#each products}}
...
{{/each}}

We use the {{#each}} block helper to sort through the products list, which is stored in the bot user variables.

That is, inside the {{#each}} block, we will consistently, over and over again, get each of the products inside the special variable this.

{{var "subtotalPrice" (multiply this.count this.cost) ~}}

Create the subtotalPrice local variable, and write to it the result of executing the multiply helper - multiplication of two values. We use this.count to get the quantity of the current product from the list inside {{#each}} and this.cost to get the cost of the current product from the list inside {{#each}}. After that, the multiplication result will be available in @vars.subtotalPrice.

{{var "totalPrice" (add @vars.totalPrice @vars.subtotalPrice) ~}}

We overwrite the TotalPrice local variable with the result of the add helper - the addition of two values.

We use the previous value of TotalPrice and add to it the value from @vars.subtotalPrice calculated by the previous expression.

{{var "totalCount" (add @vars.totalCount this.count) ~}}

Overwrite the totalCount local variable with the result of the add helper — the addition of two values.

Use the previous value of totalCount and add the value of this.count to it.

{{this.name}} — {{this.cost}} $/pieces — {{this.count}} pieces — {{@vars.subtotalPrice}} $

Output from variables an intermediate result for the current product in the cycle.

Total: {{@vars.totalPrice}} $ for {{@vars.totalCount}} pieces

Output the calculated and summed prices and the quantity of goods.

Type conversion

var records variables of the type the value was passed. I.e. if we explicitly specify a string, there will be a string if we specify a number, there will be a number.

{{#vars}}
{{var "someString" "string" ~}}
{{var "someNumber" 14 ~}}
{{/vars}}

In the first case, we created the someString local variable of string type, and in the second case we created someNumber of numeric type.

Strings in Handlebars are written in double quotes by analogy with JSON, and the numbers are written as they are.

But we can also explicitly specify the type of variable that we want to write using the var helper. To do this, use the type parameter, and specify one of the types.

var supports the main types, which Botmother also supports:

  • string
  • boolean - logical
  • number
  • json — an object from JSON

For example, we can create an empty list by explicitly saying that we want to write not a string, but an object from JSON:

{{#vars}}
{{var "list" "[]" type="json" ~}} {{@vars.list.length}}
{{/vars}}

As a result of this template, we will see 0, so by typing {{@vars.list.length}} we requested the length of our list created in @vars.list. An empty list has a length of 0.

To change the list item, you can explicitly name its index in the variable name:

{{#vars}}
{{var "list" "[]" type="json" ~}}
{{var "list.0" "Hello World" ~}}
List length: {{@vars.list.length}}
List in JSON format: {{stringify @vars.list}} {{/vars}}

If we execute this template, we will get the following result:

List length: 1
List in JSON format: ["Hello World"]

Using the json type, you can create not only empty or filled lists, but also any other variables from any correct JSON.

Consideration of JSON and data types is beyond the scope of this note, if you are not familiar with it, we recommend reading our article about data types in Botmother and about JSON in Botmother.

Complex values in var

Sometimes you need to write complex values in var, for example, collected from other templates or the values from a few lines. To do this, you can use the block version of {{#var}}.

The main difference with a lowercase var is that the value is not assigned by the second argument, but inside the {{#var}} block:

{{#vars}}
{{#var "poem"}}
A storm is blizzarding the sky,
It's a storm that's blizzarding
It will howl like a beast,
Or cries like a child
{{/var}}
{{/vars}}

Other helpers can be used inside the block version of {{#var}}:

{{#vars}}
{{var "age" 17 ~}}
{{#var "adult"}}
  {{#gte @vars.age 18 ~}}
    Adult
  {{~else~}}
    Child
  {{~/gte}}
{{/var}}
{{@vars.adult}}
{{/vars}}

The same template will output the following result.

Child

We write the age local variable for example, in a real bot it can be a user variable from User Input, Request or Assign a variable.

The block version of {{#var}} is also convenient to use for creating multi-line objects from JSON.

{{#vars}}
{{#var "user" type="json"}}
{
  "login": "admin",
  "password": "password",
  "isAdmin": true
}
{{/var}}
{{@vars.user.login}}
{{/vars}}
To the beginning ↑