Logic operators
Conditions in {if}, {elseif} and {while} are operator calls, because the template language has no ==, <,
&& or !. The 13 operators on this page are registered by eZTemplateLogicOperator
(lib/eztemplate/classes/eztemplatelogicoperator.php) through lib/eztemplate/classes/eztemplateautoload.php.
Two of their rules surprise people who know PHP or Smarty, and both are proven below:
lt,gt,leandgecompare counts, not values: a non-numeric string counts as its length, so'pear'|lt( 'apple' )is true (4 is less than 5).andandorreturn one of their operands, not always a boolean.
Contents
In the examples, choose( 'false', 'true' ) turns a boolean into a word, because a boolean true prints as 1 and
false prints nothing.
eq and ne
eq
With one parameter: whether the piped input equals it. With two or more parameters: whether all parameters are equal
(the input is not used). The comparison is PHP's loose ==, so '5'|eq( 5 ) is true. Without a parameter it logs
"Requires one parameter for input checking or two or more for parameter checking" and returns false.
| Parameter | Type | Required | Default |
|---|---|---|---|
| values | mixed | yes, one (with input) or two or more | none |
Returns a boolean. Generated reference: eq.
ne
The opposite: with one parameter, whether the input differs from it; with several, whether any two neighbors differ. Same warning without a parameter.
| Parameter | Type | Required | Default |
|---|---|---|---|
| values | mixed | yes | none |
Returns a boolean. Generated reference: ne.
5|eq( 5 ) = {5|eq( 5 )|choose( 'false', 'true' )}
'5'|eq( 5 ) = '5'|eq( 5 )|choose( 'false', 'true' )
eq( 3, 3, 3 ) = eq( 3, 3, 3 )|choose( 'false', 'true' )
eq( 3, 3, 4 ) = eq( 3, 3, 4 )|choose( 'false', 'true' )
'abc'|ne( 'abd' ) = 'abc'|ne( 'abd' )|choose( 'false', 'true' )
ne( 1, 1, 2 ) = ne( 1, 1, 2 )|choose( 'false', 'true' )Output:
5|eq( 5 ) = true
'5'|eq( 5 ) = true
eq( 3, 3, 3 ) = true
eq( 3, 3, 4 ) = false
'abc'|ne( 'abd' ) = true
ne( 1, 1, 2 ) = truelt, gt, le and ge
lt, gt, le, ge
lt (less than), gt (greater than), le (less or equal) and ge (greater or equal) compare two values: the piped
input with the named parameter threshold, or, without an input, the first parameter with the second.
Each side is first turned into a count by eZTemplateLogicOperator::comparisonCount():
| Value | Counts as |
|---|---|
null | 0 |
| boolean | 0 or 1 |
| array | its number of elements |
object with attributes() | its number of attributes |
| number or numeric string | its value |
| any other string | its length (strlen) |
Any other type logs "Unsupported input type" and leaves the value unchanged. Because of the string rule, use
strcmp or strnatcmp (see PHP string functions: search and compare) to
order words. Compiled templates call compareCounts() at run time and apply the same rule.
| Parameter | Type | Required | Default |
|---|---|---|---|
threshold | mixed | yes | false |
Returns a boolean. Generated references: lt, gt, le, ge.
3|lt( 5 ) = {3|lt( 5 )|choose( 'false', 'true' )}
gt( 10, 2 ) = gt( 10, 2 )|choose( 'false', 'true' )
5|le( 5 ) = {5|le( 5 )|choose( 'false', 'true' )}
5|ge( 6 ) = {5|ge( 6 )|choose( 'false', 'true' )}
array( 1, 2, 3 )|gt( 2 ) = array( 1, 2, 3 )|gt( 2 )|choose( 'false', 'true' )
'banana'|gt( 'apple' ) = 'banana'|gt( 'apple' )|choose( 'false', 'true' )
'pear'|lt( 'apple' ) = 'pear'|lt( 'apple' )|choose( 'false', 'true' )
'10'|gt( '9' ) = '10'|gt( '9' )|choose( 'false', 'true' )Output:
3|lt( 5 ) = true
gt( 10, 2 ) = true
5|le( 5 ) = true
5|ge( 6 ) = false
array( 1, 2, 3 )|gt( 2 ) = true
'banana'|gt( 'apple' ) = true
'pear'|lt( 'apple' ) = true
'10'|gt( '9' ) = trueand and or
Both evaluate only their parameters; the piped input is not used. Each operand is judged like this: null is
false, a boolean is itself, an array is true when it has elements, a number is true when it is not 0, an object is
true when it has attributes() and attribute() methods, and a string is true when it has something other than
whitespace (and also treats '' as false).
and
Returns false at the first false operand; when all are true it returns the last operand itself.
| Parameter | Type | Required | Default |
|---|---|---|---|
| operands | mixed | no | none |
Generated reference: and.
or
Returns the first true operand itself, or false when none is true. That makes or a fallback picker, although
first_set is clearer for that.
| Parameter | Type | Required | Default |
|---|---|---|---|
| operands | mixed | no | none |
Generated reference: or.
and( 1, 'yes', array( 1 ) ) = and( 1, 'yes', array( 1 ) )|implode( ',' )
and( 1, ' ', 3 ) = and( 1, ' ', 3 )|choose( 'false', 'true' )
or( false(), '', 'first', 'second' ) = or( false(), '', 'first', 'second' )
or( 0, array() ) = or( 0, array() )|choose( 'false', 'true' )Output:
and( 1, 'yes', array( 1 ) ) = 1
and( 1, ' ', 3 ) = false
or( false(), '', 'first', 'second' ) = first
or( 0, array() ) = falseThe whitespace rule is the interpreter's. Compiled on a development installation, and( 1, ' ', 3 ) treated the blank string as true and
returned 3, the last operand, which made the choose after it log "Index 3 out of range" and print nothing. Trim
strings before testing them ($text|trim) and the two agree.
Output with the template compiler (compiled into a scratch folder):
and( 1, 'yes', array( 1 ) ) = 1
and( 1, ' ', 3 ) =
or( false(), '', 'first', 'second' ) = first
or( 0, array() ) = falsenot, null, true and false
not
Negates the piped input, or the first parameter when there is no input. "Empty" means: null, an empty array, an
object without attributes, a number or numeric string equal to 0 (so '0' too), or an empty string. Returns a boolean.
| Parameter | Type | Required | Default |
|---|---|---|---|
| value | mixed | no | the input |
Generated reference: not.
null
Whether the piped input is exactly null (===). A fetch that finds nothing returns null. No parameters.
Generated reference: null.
true and false
Return the booleans true and false. They are how you write a boolean literal: true(), false(). No parameters.
Generated references: true, false.
{def $empty = array()
$node = fetch( 'content', 'node', hash( 'node_id', 89 ) )
$missing = fetch( 'content', 'node', hash( 'node_id', 999999999 ) )}
$empty|not = $empty|not|choose( 'false', 'true' )
not( 0 ) = not( 0 )|choose( 'false', 'true' )
'0'|not = '0'|not|choose( 'false', 'true' )
''|not = ''|not|choose( 'false', 'true' )
$node|not = $node|not|choose( 'false', 'true' )
$missing|null (no node has that id) = $missing|null|choose( 'false', 'true' )
true() = true()|choose( 'false', 'true' ), false() = false()|choose( 'false', 'true' )Output:
$empty|not = true
not( 0 ) = true
'0'|not = true
''|not = true
$node|not = false
$missing|null (no node has that id) = true
true() = true, false() = falsechoose
choose
Picks one of its parameters by the piped input: a number is the index (0 picks the first parameter), a boolean is 0
or 1 (so false picks the first and true the second), and null is 0. Only the chosen parameter is evaluated.
An array or object input logs "Only supports numeric and boolean values"; an index outside the parameters logs
"Index ... out of range" and returns false.
| Parameter | Type | Required | Default |
|---|---|---|---|
| choices | mixed | yes | none |
Generated reference: choose.
{def $n = 2
$node = fetch( 'content', 'node', hash( 'node_id', 144 ) )}
$n|choose( 'zero', 'one', 'two' ) = $n|choose( 'zero', 'one', 'two' )
true()|choose( 'no', 'yes' ) = true()|choose( 'no', 'yes' )
The Recipes node $node.children_count|gt( 0 )|choose( 'has no children', concat( 'has ', $node.children_count, ' children' ) ).Output:
$n|choose( 'zero', 'one', 'two' ) = two
true()|choose( 'no', 'yes' ) = yes
The Recipes node has 18 children.An index outside the list is an error:
3|choose( 'a', 'b' ) = {3|choose( 'a', 'b' )|choose( 'false', 'true' )}Output:
3|choose( 'a', 'b' ) = falseLogged by the template engine:
ERROR choose: Index 3 out of range 0 => 1For a two-way choice on any condition, cond reads more naturally.
Related pages
- Logic and comparison operators for designers
- If and conditions
- Control operators: cond and first_set
- Type operators:
is_set,is_arrayand the other tests - All template operators (generated)
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.