EN IT
Open2b version 7.8

Operators

Scriggo provides several arithmetic, comparison, and logical operators. Because Scriggo is strongly typed, each operator can be used only with certain data types, and for operators with two operands, they must be of the same type. The exceptions are the three logical operators and, or, and not, for which operands can be of any type, and the contains operator which can be used with specific types.

In addition to these operators there is also the operator render for rendering a partial file.

Basic operators

The following are Scriggo’s basic operators:

== Equality
!= Inequality
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
|| Logical or
&& Logical and
! Negation
+ Addition and concatenation
- Subtraction
* Multiplication
/ Division
% Modulo

Example:

{% if stock > 10 %}
High availability
{% end %}

The following table shows which operators can be used with which data types:

Data type Operators
Numeric == != < <= > >= + - * /
Integer %
String == != < <= > >= +
Boolean == != ! && ||

And, or and not

Scriggo allows you to apply "and", "or" and "not" to any expression of any type using the and, or and not operators. Their evaluation returns true or false based on the truthiness of their operands.

The and, or and not operators are not present in Go but make simple if conditions easier to read for a first approach to the Scriggo template system.

For stricter type checking, use the Go logical operators &&, || and !, since their operands must be boolean and, if there are two, they must be of the same boolean type.

{% if promotion and stock %} offer with immediate availability {% end %}
{% if not price %} sign up to see the price {% end %}

"true" or "false"

All values are “true” except the following which are “false.”

Data type "false" value
bool false
int 0
float64 0.0
string ""
[]string nil or empty
Decimal decimal(0)
Product Product{}

Additionally, any value on which you can call the Zero method and which returns true is “false.”

Contains

The boolean operators contains and not contains indicate whether a slice contains or does not contain an element, a map contains or does not contain a key, or a string contains or does not contain a substring or rune. The two operators have the following form:

a contains b

a not contains b

The contains operators are Scriggo-specific and are not present in Go.

Examples

Check whether a slice of strings contains a string:

{% var colors = []string{"red", "blue", "yellow", "green"} %}
{% if colors contains "yellow" %}
  colors contains "yellow"
{% end %}
colors contains "yellow"

Check whether a string contains a substring:

{% if product.Name contains "bundle" %}
  the product name contains the word "bundle"
{% end %}

Check whether a map contains a key:


{% var nameOf = map[int]string{1: "one", 4: "four", 7: "seven"} %}
{% if nameOf contains 7 %}
  nameOf contains the key 7
{% end %}
nameOf contains the key 7