EN IT
Open2b version 7.8

Data types

All values in Scriggo have a type and there are several basic types.

Go data types

Scriggo implements all data types of the Go language. In this documentation, however, only the types most commonly used in a template are documented, but there is no limitation on using any other Go type.

Booleans

Boolean values are true and false. A boolean variable can be declared in one of the following ways:

{% var a bool %}     a is false
{% var b = false %}  b is false
{% var c = true %}   c is true

String

Strings are written with double quotes "hello" or with backticks `hello`. A string variable can be declared in one of the following ways:

{% var a string %}    a is an empty string
{% var b = "" %}      b is an empty string
{% var c = "hello" %}  c is the string "hello"

To read the byte length of a string, use len; to read its length in “characters,” use runeCount.

Runes

The function name runeCount comes from "rune", which is the term Go uses for the “characters” of a string. For more information you can read Strings, bytes, runes and characters in Go.

Numeric

Scriggo has many numeric types; the most common in templates is int. There is also the Decimal type, which represents fixed‑point decimal numbers useful for monetary calculations without loss of precision.

int

Values of type int are integers (more precisely they range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). An int variable can be declared in one of the following ways:

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

Decimal

Values of type Decimal are fixed‑point decimals, and to create one you use the decimal function. A Decimal variable can be declared in one of the following ways:

{% var a Decimal %}             a is the decimal number 0.0
{% var b = Decimal{} %}         b is the decimal number 0.0
{% var c = decimal("12.95") %}  c is the decimal number 12.95

Slice

A slice is an indexed sequence of elements of a given type. For example []int{5, 2, 7, 9} is a slice of int and []string{"hello", "ciao"} is a slice of strings. A slice can be nil, which is different from an empty slice. A slice variable can be declared in one of the following ways:

{% var a []int %}                a is a nil int slice
{% var b []int = nil %}          b is a nil int slice
{% var c = []int{} %}            c is an empty int slice
{% var d = []int{3, 0, 7, 2} %}  d is an int slice with elements 3, 0, 7 and 2

Accessing an element of a slice

To access an element of a slice, use square brackets [ and ] with a zero‑based index. For example:

{% var s = []string{"a", "b", "c"} %}
{% s[1] = "e" %}
{{ s[0] }} {{ s[1] }} {{ s[2] }} 
a e c

To access the last element of a slice s you can write s[len(s)-1].

Accessing a nil slice or a non‑existent index is an error.

Iterating over slice elements

To iterate over slice elements, use the for instruction in one of its forms. For example:

{% var saluti = []string{"Ciao", "Hello", "你好"} %}

{% for saluto in saluti %}
{{ saluto }}
{% end %}
Ciao Hello 你好

Slice length

To read the length of a slice, i.e. the number of elements it contains, use the len function:

{% var s = []int{"a", "b", "c"} %}
{{ len(s) }}
3

Slicing

Slicing is an operation that returns a portion of a slice. To slice, use square brackets [ and ] with a start index and an end index (exclusive) separated by a colon :.

Examples

{% var s = []string{"a", "b", "c", "d", "e"} %}
{{ s[1:3] }}
b c
{% for e in s[2:5] %}
{{ e }}
{% end %}
c d e

Slicing does not copy elements, so the resulting slice refers to the same elements as the original slice:

{% var s = []int{0, 1, 2, 3, 4, 5} %}
{% var p = s[0:3] %}
{% s[0] = 5 %}
{{ p }}
5 1 2

Appending elements to a slice

The length of a slice cannot change, but you can slice to get a shorter slice and you can append to get a longer slice. You append using the append function:

{% var s = []int{0, 1} %}
{% s = append(s, 2, 3, 4, 5) %}
{{ s }}
0 1 2 3 4 5

append always returns a new slice, but unlike slicing you should not assume the returned slice refers to the same elements as the original slice because it might refer to a copy.