Conditions and Branching

The {if} pair renders its content when its expression holds, with as many {elseif} branches as needed and an {else} for when none did:

PHP
HTML
fragments/conditions_1.pad
Result
{data 'nums'}[3, 7, 12]{/data}
{nums}
<p>{$nums} is
{if $nums lt 5}
small
{elseif $nums lt 10}
medium
{else}
large
{/if}
</p>
{/nums}

3 is small

7 is medium

12 is large

The comparison operators

operator meaning
eq or == equal
ne or != not equal
gt / lt greater / less than
ge / le greater / less or equal
and / or both hold / either holds
range (a, b) the value lies between a and b, ends included

 

PHP
HTML
fragments/conditions_2.pad
Result
{set $age = 35/}
<p>{if $age range (18, 65)}Working age.{/if}</p>
<p>{if $age gt 30 and $age lt 40}In the thirties.{/if}</p>
<p>{if $age lt 18 or $age gt 65}Not working age.{else}Still is.{/if}</p>

Working age.

In the thirties.

Still is.

A bare value is tested for its truth. {if $flag} holds unless the value is empty, 0, '0' or FALSE - the truth PHP gives it; write a comparison when a particular value is meant: {if $count eq 0}.

Case and when

When one value decides between several branches, {case} reads better than a chain of elseifs. Each {when} names a value, and {else} catches the rest:

PHP
HTML
fragments/conditions_3.pad
Result
{set $light = 'green'/}
{case $light}
{when 'red'}Stop
{when 'yellow'}Caution
{when 'green'}Go
{else}Unknown
{/case}
Go

A {when} may list several values, separated by commas; it answers when any of them matches:

PHP
HTML
fragments/conditions_7.pad
Result
{data 'days'}["Saturday","Monday","Sunday"]{/data}
{days}
<p>{$days}: {case $days}{when 'Saturday', 'Sunday'}weekend{else}a working day{/case}</p>
{/days}

Saturday: weekend

Monday: a working day

Sunday: weekend

The ternary

Inside an expression tag, ? and : pick one of two values - the usual way to give alternating rows their class, together with the iteration properties:

PHP
HTML
fragments/conditions_4.pad
Result
{data 'fruit'}["apple","pear","plum"]{/data}
{fruit}
<p class="{odd@fruit ? odd : even}">{$fruit}</p>
{/fruit}

apple

pear

plum

Inside an expression

An expression has a ternary of its own, cond ? then : else, the weakest binding there is. a ?? b answers a unless it is empty or a field that is not there - so it gives a field its default - and the tests in, like, matches and range read infix:

PHP
fragments/conditions_6.php
HTML
fragments/conditions_6.pad
Result
<?php

$count
= 3;
$heading = '';
$code = 'NL1234';
$state = 'pending';

?>
<p>{$count} {echo $count eq 1 ? 'item' : 'items'}</p>
<p>Title: {$heading | ?? 'Untitled'} - subtitle: {echo $subtitle ?? 'none'}</p>
<p>Code {$code} {if $code matches '/^[A-Z][A-Z][0-9]+$/'}is{else}is not{/if} well formed</p>
<p>State {$state} {if $state in ('active', 'pending')}is open{else}is closed{/if}</p>

3 items

Title: Untitled - subtitle: none

Code NL1234 is well formed

State pending is open

Properties in conditions

The iteration properties read as values inside an expression, so they stand in conditions - alone for the boolean ones, with an operator for the counting ones:

PHP
HTML
fragments/conditions_5.pad
Result
{data 'crew'}["Kirk","Spock","McCoy"]{/data}
{crew}
{if first@crew}<ul>{/if}
<li>{$crew}{if notLast@crew} and counting{/if}</li>
{if last@crew}</ul>{/if}
{/crew}
  • Kirk and counting
  • Spock and counting
  • McCoy

Every tag also carries the else construct: the part after the divider renders when the tag itself produced nothing.