{classes}
Builds a class list from fixed names and names that are in only while their condition holds.
Syntax
{classes 'fixed names', name=condition, other=condition}
{classes $array}
Parameters
-
items -
Any number of items: an expression - a text of one or more names, or an array of them - always in;
name=conditionputs that name in when the condition holds.
Description
{classes} writes the value of a class attribute: <div class="{classes 'panel', active=$isActive, invalid=$hasErrors}">. An item without a name is always in - a text of one or more names separated by spaces, or an array of them. An item name=condition adds the name when the condition is true; an empty array counts as false. The result is deduplicated, in the order the names first appear, and escaped.
It replaces the chains of {if} that conditional classes otherwise need, and the names may contain dashes - is-open=$open. Like {attrs} it reads its items raw, so any name is a class name, never an engine option. Inside a loop, a property reads as a condition: first=first@items.
Examples
Fixed and conditional names
panel is always in; active and invalid only when their condition holds - a repeated name is written once.
{set $isActive = TRUE}
{set $hasErrors = FALSE}
<div class="{classes 'panel panel-large', active=$isActive, invalid=$hasErrors, 'panel'}">The panel</div>
<pre>class="{classes 'panel panel-large', active=$isActive, invalid=$hasErrors, 'panel'}"</pre>
class="panel panel-large active"
Per row of a loop
Each row's classes from its own fields and from the loop's properties.
<ul>
{tasks}
<li class="{classes 'task', done=$done, first=first@tasks, urgent=$days lt 2}">{$title}: <code>{classes 'task', done=$done, first=first@tasks, urgent=$days lt 2}</code></li>
{/tasks}
</ul>
<?php
$tasks = [
[ 'title' => 'Write the release notes', 'done' => TRUE, 'days' => 0 ],
[ 'title' => 'Update the screenshots', 'done' => FALSE, 'days' => 1 ],
[ 'title' => 'Plan the next version', 'done' => FALSE, 'days' => 14 ] ];
?>
- Write the release notes:
task done first urgent - Update the screenshots:
task urgent - Plan the next version:
task
See also
- {attrs} Writes HTML attributes from values - quoted and escaped, a true boolean attribute bare, a false one left out.
- {switch} Answers the next value of a rotating list on every call - striped rows without a counter.
- {if} Renders its content only when a condition holds, with elseif and else branches.