{data}
Turns its content - JSON, XML, YAML, CSV, a list or a range - into a named data set for a later tag to iterate.
Syntax
{data 'name'} ... {/data}
{data 'name', type='csv'} ... {/data}
{data 'name', source}
{data} ... {/data 'name'}
Parameters
-
name -
The name the data is kept under;
{name}...{/name}(or{data:name}) iterates it. Not the name of a built-in or application tag. -
source -
Optional, when there is no content: the data as a value -
'1..5','["a","b"]'- or a field holding it.
Options
-
type -
The format of the content -
json,xml,yaml,csv,list,range... - when its first characters do not tell.
Description
{data 'name'} ... {/data} reads its content as data and keeps it under that name; it prints nothing. A later {name} ... {/name} iterates it like any page array - fields, properties, the handling options (sort, where, first ...) - and so do data='name' on another tag, {data:name}, {tree} and the lookup pipe.
Formats
The format is recognised from the content itself:
-
JSON - starts with
[or{. An array of objects gives rows with fields; a plain array gives values, read with{$name}. -
XML - starts with
<. The elements under the root are the rows, their attributes and child elements the fields. -
YAML - starts with
---, or with a- itemorkey: valueline. It needs PHP's yaml extension. -
A list -
('a', 'b', 2 * 3)in parentheses: every element is evaluated as an expression. -
A range -
1..10ora..z. - CSV - anything else of several lines: the first line names the fields, each further line is a row.
When the start does not tell, type='csv' (or another format) says it. A one-line name of a file in the application's _data/ directory reads that file.
Other forms
Without content, a second parameter is the source: {data 'steps', '1..5'}. A value from a field there is data, never template: a field holding a parenthesised list is refused, since its elements would run as expressions. With the name on the closing tag - {data} ... {/data 'name'} - the content is rendered first and the result read as data, so fields and tags inside it are resolved.
Content with braces - JSON objects - is read as written: a JSON object in the content is not taken for a tag. Under the strict syntax check a {data} without a name is an error, and so is a name that is already a built-in or application tag, since {name} would never reach the store.
Examples
JSON rows
An array of objects becomes rows with fields, iterated by the name it was given.
{data 'dataPlanets'}
[
{ "name": "Mercury", "moons": 0 },
{ "name": "Earth", "moons": 1 },
{ "name": "Mars", "moons": 2 }
]
{/data}
<ul>
{dataPlanets}
<li>{$name}: {$moons} {echo $moons eq 1 ? 'moon' : 'moons'}</li>
{/dataPlanets}
</ul>
- Mercury: 0 moons
- Earth: 1 moon
- Mars: 2 moons
CSV and XML
The first CSV line names the fields; XML elements under the root are the rows, attributes the fields.
{data 'dataPrices'}
fruit,price
Apple,0.40
Pear,0.55
{/data}
{data 'dataStock'}
<stock>
<item fruit="Apple" boxes="12"/>
<item fruit="Pear" boxes="3"/>
</stock>
{/data}
<p>{dataPrices}{$fruit} costs {$price}. {/dataPrices}</p>
<p>{dataStock}{$fruit}: {$boxes} boxes. {/dataStock}</p>
Apple costs 0.40. Pear costs 0.55.
Apple: 12 boxes. Pear: 3 boxes.
Ranges, lists and a source parameter
A range, an evaluated list, and data given as the second parameter instead of content.
{data 'dataLetters'}a..f{/data}
{data 'dataSums'}(1 + 1, 2 * 3, 10 - 7){/data}
{data 'dataSteps', '1..4'}
<p>{dataLetters}{$dataLetters} {/dataLetters}</p>
<p>{dataSums}{$dataSums} {/dataSums}</p>
<p>{dataSteps}step {$dataSteps}{notLast@dataSteps}, {/notLast@dataSteps}{/dataSteps}</p>
a b c d e f
2 6 3
step 1, step 2, step 3, step 4
See also
- {content} Keeps a piece of template under a name instead of printing it, for a later {name} to render.
- {bool} Reduces its content or a value to TRUE or FALSE and keeps it under a name, for a later {name} to test.
- {set} Assigns one or more variables, as globals that outlive the level the tag stands in.
- {tree} Renders a tree of rows: its body for every row, and {recurse} renders it again for the row's children.
- {count} Renders its content only when a named data set or page array holds at least one element.