{bool}
Reduces its content or a value to TRUE or FALSE and keeps it under a name, for a later {name} to test.
Syntax
{bool 'name'} expression {/bool}
{bool 'name', value}
{name} ... @else@ ... {/name}
Parameters
-
name -
The name the flag is kept under;
{name}...{/name}or{bool:name}...{/bool:name}renders its content when it is TRUE. Not the name of a built-in or application tag. -
value -
Optional, when there is no content: a value whose truth is the flag - blank and
'0'are FALSE, anything else TRUE. With neither content nor value the flag is FALSE.
Description
{bool 'name'} ... {/bool} works out a yes or no once and keeps it under a name; the tag prints nothing. The content is read as an expression - $stock gt 0, 1, $isAdmin and $active - and its truth is the flag: blank content, 0 or a false comparison give FALSE, anything else TRUE.
Later, {name} ... {/name} (or {bool:name} ... {/bool:name} when the name is also something else) renders its content when the flag is TRUE and its @else@ part when it is FALSE. In an expression the flag is read with flag:name. The flag keeps the value it had when it was stored, even when the variables in the expression change afterwards.
Without content, a second parameter gives the value: {bool 'open', $hours} - the value's own truth by PHP's rule, so blank and '0' are FALSE and anything else TRUE. A value is never run as template or expression. With neither content nor value the flag is FALSE.
Under the strict syntax check a {bool} without a name, or with the name of a built-in or application tag, is an error. For a test used once, {if} is simpler; a flag pays off when the same test is asked in several places.
Examples
A flag tested twice
The expression is evaluated once; the flag answers in two places, with an @else@ part.
{set $stock = 3}
{bool 'boolInStock'}$stock gt 0{/bool}
<p>{boolInStock}In stock.@else@Sold out.{/boolInStock}</p>
<p>{boolInStock}<button>Add to cart</button>@else@<button disabled>Notify me</button>{/boolInStock}</p>
In stock.
From a value
Without content the second parameter's own truth is the flag: blank and 0 are FALSE.
{bool 'boolName', 'Ann'}
{bool 'boolZero', 0}
{bool 'boolBlank', ''}
<ul>
<li>'Ann': {boolName}TRUE@else@FALSE{/boolName}</li>
<li>0: {boolZero}TRUE@else@FALSE{/boolZero}</li>
<li>blank: {boolBlank}TRUE@else@FALSE{/boolBlank}</li>
</ul>
- 'Ann': TRUE
- 0: FALSE
- blank: FALSE
See also
- {if} Renders its content only when a condition holds, with elseif and else branches.
- {content} Keeps a piece of template under a name instead of printing it, for a later {name} to render.
- {data} Turns its content - JSON, XML, YAML, CSV, a list or a range - into a named data set for a later tag to iterate.
- {true} The constant TRUE: its content renders once, as a level of its own.
- {false} The constant FALSE: its content is skipped and its @else@ part renders instead.