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:
The following operators are available in OPT. Some of them have two variants: symbolic and text.
Table 2.1. List of operators
Symbolic | Text | Example | Description |
---|---|---|---|
== | eq | $a == $b | true if $a is equal to $b |
!= | neq | $a != $b | true if $a is not equal to $b |
=== | $a === $b | true if $a is equal to $b and both have the same type | |
!== | $a !== $b | true if $a is not equal to $b or they have different types | |
> | gt | $a > $b | true if $a is greater than $b |
< | lt | $a < $b | true if $a is lower than $b |
>= | gte, ge | $a >= $b | true if $a is greater or equal to $b |
<= | lte, le | $a <= $b | true if $a is lower or equal to $b |
&& | and | $a && $b | true if both $a and $b are true |
|| | or | $a || $b | true if $a or $b is true |
xor | $a xor $b | true if $a or $b is true, but not both. | |
! | not | ! $a | true if $a is false |
+ | add | $a + $b | sum of $a and $b |
- | sub | $a - $b | difference of $a and $b |
* | mul | $a * $b | product of $a and $b |
/ | div | $a / $b | quotient of $a and $b |
% | mod | $a % $b | remainder of $a divided by $b |
++ | $a++ | increments the value of $a by 1 | |
-- | $a-- | decrements the value of $a by 1 | |
:: | $a :: $b | concatenation of strings $a and $b | |
= | is | @a is $b | assigns 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.