2.4. Expressions

Blocks and variables can be combined in bigger structures called expressions. Let's say that the programmer passed to the template the amount of active and inactive users. Can you still display the total number of accounts? The answer is: yes. Take a look at the example:

Example 2.5. Variables

<p>Active accounts: {$active}</p>
<p>Inactive accounts: {$inactive}</p>
<p>Total accounts: {$active + $inactive}</p>

As you see, the blocks are connected with an operator that sums the values and returns the result. OPT allows to create compound expressions built of many different operators.

An expression contains:

  1. Numbers in decimal and hexadecimal format: 1234567890 or 0x123456789ABCDEF0 or 0x123456789abcdef0
  2. Strings enclosed with:
    1. Single quotes: 'this is a text'
    2. Double quotes: "this is a text"
    3. Reversed quotes: `this is a text`
    4. Single words may be written without quotes, only if they are not operators and are not located on an operator place.
    5. To write a quote as a part of text, you follow it with backslash: "double \" quote in a text"
  3. Operators: connect two smaller expressions and return the value. The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. The precendence in OPT is the same, as in PHP.
  4. Functions - they take some parameters (arguments) and generate a result for them, like in maths or PHP. The syntax: functionName(parameters) where parameters is a list of expressions divided with a comma. For example, the function foo(1, 2, 3) takes three parameters: 1, 2 and 3. bar() does not take any parameters. Similar syntax is used for object methods.

The following operators are available in OPT. Some of them have two variants: symbolic and text.

Table 2.1. List of operators

List of operators
SymbolicTextExampleDescription
==eq$a == $btrue if $a is equal to $b
!=neq$a != $btrue if $a is not equal to $b
=== $a === $btrue if $a is equal to $b and both have the same type
!== $a !== $btrue if $a is not equal to $b or they have different types
>gt$a > $btrue if $a is greater than $b
<lt$a < $btrue if $a is lower than $b
>=gte, ge$a >= $btrue if $a is greater or equal to $b
<=lte, le$a <= $btrue if $a is lower or equal to $b
&&and$a && $btrue if both $a and $b are true
||or$a || $btrue if $a or $b is true
 xor$a xor $btrue if $a or $b is true, but not both.
!not! $atrue if $a is false
+add$a + $bsum of $a and $b
-sub$a - $bdifference of $a and $b
*mul$a * $bproduct of $a and $b
/div$a / $bquotient of $a and $b
%mod$a % $bremainder of $a divided by $b
++ $a++increments the value of $a by 1
-- $a--decrements the value of $a by 1
:: $a :: $bconcatenation of strings $a and $b
=is@a is $bassigns the right value to the variable on the left

Note: OPT supports only post-increment and post-decrement operators. The syntax ++$a may cause unexpected behaviour.

The table may look a bit confusing, so now we are going to take a tour on an example. There is an instruction called "if" that allows to show a part of the template, if the specified condition is true. The script sends us two blocks: $name and $surname - we want to show additional text, if they both point to a certain person.

Example 2.6. Simple expressions

{if $name == "John" and $surname == "Brown"}
	<p>Hey John, how are you!</p>
{/if}

Such operators, as > and == can be used to compare some data, whereas and, or define relations between them. Another example will be a result of some internet test divided into three stages. The competitor takes one, and then chooses another from two possibilities. The script sends to our template the following blocks: $test1 - a number of points in test 1, $test2 in test 2, and $test3 in test 3. We want to display additional congratulations, if he passed the first test, and another one of the twos.

Example 2.7. More complicated expression

{if $test1 > 50 and ($test2 > 50 or $test3 > 50)}
	<p>Congratulations, you have passed our test!</p>
{/if}

As you see, we used brackets to group the conditions for test 2 and 3 into one bigger expression. Now the parser knows that the results in test 1 and the one chosen by the competitor must be greater than 50. Without the brackets, it would be calculated in the following way: test 1 and test 2, or just test 3, must be greather than 50. It is completely different task that we wanted.