Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,059 of 9,059 pages
About the help in other languages
All help pages

Control operators

cond and first_set from eZTemplateControlOperator - inline conditions and fallbacks that evaluate only what they need, how the interpreter and the compiled template differ for a false default, with examples rendered on…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/help/template-operators/control-operators.md

On this page

Control operators

cond and first_set are the template language's answer to a ternary (a ? b : c) and a null-coalescing (a ?? b) expression. Both are registered by eZTemplateControlOperator (lib/eztemplate/classes/eztemplatecontroloperator.php) through lib/eztemplate/classes/eztemplateautoload.php. Neither takes a piped input nor named parameters; both evaluate their parameters lazily, from left to right, and stop as soon as they have an answer. The compiler turns both into nested PHP if blocks (condTransform()), so they cost no operator call in a compiled template.

Contents

  1. cond
  2. first_set
  3. Related pages

cond

cond

Takes pairs of parameters, condition and value. It returns the value of the first pair whose condition is true. An odd last parameter is the default, used when no condition matched. With a single parameter it returns that parameter.

ParameterTypeRequiredDefault
condition, value pairsmixedyesnone
default (odd last parameter)mixednonone

Input: none. Returns the chosen value, or nothing when no condition matched and there is no default. Generated reference: cond.

{def $node = fetch( 'content', 'node', hash( 'node_id', 144 ) )}
{cond( $node.children_count|gt( 10 ), 'many', $node.children_count|gt( 0 ), 'some', 'none' )} children ({$node.children_count})
{cond( false(), 'never', 'the default' )}
{cond( 'only one parameter' )}

Output:

many children (18)
the default
only one parameter

A common use is a class attribute in a loop, here on the children of the Fit & Healthy front page (node 89):

{foreach fetch( 'content', 'list', hash( 'parent_node_id', 89, 'limit', 4, 'sort_by', array( 'priority', true() ) ) ) as $i => $child}
<li class="{cond( $i|eq( 0 ), 'first', $child.class_identifier|eq( 'ng_topic' ), 'topic', 'item' )}">{$child.name|wash}</li>
{/foreach}

Output:

<li class="first">Fitness</li><li class="topic">Workout</li><li class="item">Fit &amp; Healthy site info - [SITEINFO]</li><li class="item">Tour</li>

A default that is itself false. The interpreter (modify()) evaluates the default like a condition and uses it only when it is true, so a default of 0, '' or false() gives nothing. The compiled form assigns the default without testing it. The example below was rendered by the interpreter:

[{cond( false(), 'x', 0 )}]

Output:

[]

Compiled on a development installation, where template compiling is enabled, the same line printed [0].

Output with the template compiler (compiled into a scratch folder):

[0]

To get a reliable 0, test the case explicitly: cond( $x, 'x', true(), 0 ).

first_set

first_set

Returns the first parameter that is set, that is, not null. Undefined variables and missing hash keys are allowed here and log nothing (the interpreter asks for them with the existence check of elementValue(), the compiled code uses isset()). When none is set it returns null. A constant parameter ends the search in the compiled code, so put the literal fallback last.

ParameterTypeRequiredDefault
candidatesmixedyesnone

Input: none. Returns the first value that is not null. Note that 0, '' and false count as set. Generated reference: first_set.

{def $view_parameters = hash( 'offset', 20 )}
offset = {first_set( $view_parameters.offset, 0 )}
limit = {first_set( $view_parameters.limit, $undefined_limit, 10 )}
title = {first_set( $no_such_variable, 'Untitled' )}

Output:

offset = 20
limit = 10
title = Untitled

This is the usual way to read an optional parameter of an included template or an optional view parameter such as $view_parameters.offset.

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.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x