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 |
|---|---|---|
|
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 |
|---|---|---|
|
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 |
|---|---|---|
|
Go |
A {when} may list several values, separated by commas; it answers when any of them matches:
|
PHP |
HTML fragments/conditions_7.pad |
Result |
|---|---|---|
|
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 |
|---|---|---|
|
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 |
|---|---|---|
|
|
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 |
|---|---|---|
|
|
Every tag also carries the else construct: the part after the divider renders when the tag itself produced nothing.