{count}

Renders its content only when a named data set or page array holds at least one element.

Variables and stores Tag pairs

Syntax

{count 'name'} ... {/count}
{count 'name'} ... @else@ ... {/count}

Parameters

name
The quoted name of a {data} set or of a page array. A data set of that name goes before a variable.

Description

{count 'orders'} is an "is there anything in it" test: its content renders when the data set or the page array named orders holds at least one element, and its @else@ part renders when it is empty. A typical use is a heading and a table that only appear when there are rows, with a "nothing found" text otherwise.

Despite its name the tag prints no number. The number of rows of a loop is the count@orders property, readable inside the loop and in conditions. The name is looked up as a {data} set first, then as a variable of the page. A variable that does not exist, or is NULL, holds nothing; one that holds a single plain value counts as holding that value.

Write the name quoted: {count 'orders'}. {count $orders} evaluates the array itself as the name, which is an error. Inside a loop, the data tag's own @else@ part already shows when it has no rows - {count} is for markup around the loop.

Examples

A table only when there are rows

The heading and table render for a list with rows; an empty list gets the @else@ text.

Templateexamples/count/1-table.pad
{count 'countOpen'}
  <h4>Open orders</h4>
  <table>{countOpen}<tr><td>{$number}</td><td>{$customer}</td></tr>{/countOpen}</table>
@else@
  <p>No open orders.</p>
{/count}
{count 'countLate'}
  <h4>Late orders</h4>
@else@
  <p>No late orders.</p>
{/count}
PHPexamples/count/1-table.php
<?php

  $countOpen = [
    [ 'number' => 'A-101', 'customer' => 'Ann' ],
    [ 'number' => 'A-102', 'customer' => 'Bob' ] ];

  $countLate = [];

?>
Resultopen alone

Open orders

A-101 Ann
A-102 Bob

No late orders.

A data set, and the number of rows

{count} tests a {data} set; the number itself is the count@ property.

Templateexamples/count/2-data.pad
{data 'countTasks'}["Write", "Review", "Publish"]{/data}
{count 'countTasks'}
  <p>{countTasks}{first@countTasks}There are {count@countTasks} tasks: {/first@countTasks}{$countTasks}{notLast@countTasks}, {/notLast@countTasks}{/countTasks}.</p>
{/count}
Resultopen alone

There are 3 tasks: Write, Review, Publish.

See also

  • {data} Turns its content - JSON, XML, YAML, CSV, a list or a range - into a named data set for a later tag to iterate.
  • {if} Renders its content only when a condition holds, with elseif and else branches.
  • {bool} Reduces its content or a value to TRUE or FALSE and keeps it under a name, for a later {name} to test.