{classes}

Builds a class list from fixed names and names that are in only while their condition holds.

Text and formatting Single tags

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=condition puts 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.

Templateexamples/classes/1-conditions.pad
{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>
Resultopen alone
The panel
class="panel panel-large active"

Per row of a loop

Each row's classes from its own fields and from the loop's properties.

Templateexamples/classes/2-loop.pad
<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>
PHPexamples/classes/2-loop.php
<?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 ] ];

?>
Resultopen alone
  • 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.