EN IT
Open2b version 7.8

Variables

Scriggo lets you declare new variables in the template in addition to global variables.

Declaration

A variable must be declared with the var instruction before it can be used:

{% var welcome = "Hi" %}

Variables declared in template code can be used the same way as global variables:

{% var amount = 50 %}

Free shipping applies for orders of at least {{ amount }} euros
Free shipping applies for orders of at least 50 euros

Variable types

Each variable has a type, and its type is the type of the value assigned at declaration. The type can also be specified explicitly; for example, the following declarations are equivalent and all three variables will have value 0:

{% var a = 0 %}
{% var b int %}
{% var c int = 0 %}

The type of a variable cannot change; it determines which values can be assigned, which operators can be used, and which methods can be called on it.

Examples

{% var a string %}    a has type string and value ""
{% var b int %}       b has type int and value 0
{% var c bool %}      c has type bool and value false
{% var d Decimal %}   d has type Decimal and value Decimal{}, i.e. decimal 0.0
{% var e []string %}  e has type []string and value nil

The following are like the previous declarations but with an initial value:

{% var a = "hello" %}                    a has type string and value "hello"
{% var b = 5 %}                          b has type int and value 5
{% var c = true %}                       c has type bool and value true
{% var d = decimal("4.7") %}             d has type Decimal and value decimal 4.7
{% var e = []string{"hello", "world"} %}  e has type []string and value []string{"hello", "world"}

Assignment

Once a variable has been declared, you can change its value at any time with an assignment. The type cannot change, so only values of the same type can be assigned.

{% welcome = "hello" %}

There are several other assignment instructions that combine assignment with an operator:

Write is equivalent to
a++ a = a + 1
a-- a = a - 1
a += b a = a + b
a -= b a = a - b
a *= b a = a * b
a /= b a = a / b
a %= b a = a % b

Visibility

Global variables are visible in any file and in any part of the code. Variables declared in template code are visible only from the point where they are declared and only within the same file, and in some cases their visibility is even more limited.

Variables declared inside an if, else, else if, switch, case, default, for, and macro instruction are further limited to the instruction in which they are declared.

Examples

In the following example the n variable is visible both outside and inside the if instruction, while the s variable is visible only inside it.

{% var n = 3 %}
{% if n > 2 %}
  {% var s = "hello" %}
  {{ n }}{{ s }}
{% end if %}
{{ n }}

In this other example, the n variable will not be visible in the "column.html" file.

{% var n = 3 %}
{{ render "column.html" }}

To make its value available in another file you can use a macro. In this example the value of the n variable is passed as an argument to the Column macro, which can also be declared in another file:

{% var n = 3 %}
{{ Column(n) }}

Multiple declarations and assignments

The var declaration and = assignment instructions also allow you to declare and assign multiple variables at the same time. Writing:

{% var n, s = 5, "hello" %}

{% n, s = 2, "hello" %}

is equivalent to:

{% var n = 5 %}
{% var s = "hello" %}

{% n = 2 %}
{% s = "hello" %}

Multiple assignment is useful, for example, to swap the values of two variables:

{% a, b = b, a %}

And multiple declaration and assignment are useful to assign the return values of a function that returns two or more values:

{% var d, err = parseDecimal("12.99") %}