eXtension Templates for PHP (XTP for short)
Builtin controls
You don't need to specify anything in template prologue to be able to use built-in controls. These are always available with prefix "php".
RepeaterX
RepeaterX control is available to make it easier for you to create repeating blocks of HTML (in order to avoid using loops). RepeaterX itself can contain up to five inner template blocks:
- HeaderTemplate - template used to render its header - this template is rendered first, even if there are no items to render;
- ItemTemplate - this template is used to render items from repeater's data source;
- SeparatorTemplate - contents of this template is used to separate items from repeater's data source;
- AlternatingItemTemplate - if specified this template is used to render alternating items, if not specified all items are rendered using ItemTemplate;
- FooterTemplate - this template is used to render footer - this template is rendered last, even if there are no items to render.
- DataSource - this is source of items to display in repeater - you should always specify this prior to rendering content of repeater (otherwise you will get empty repeater);
- Item - this is item being currently rendered - you can use this within any of repeater's templates;
- Index - this is index (starting with 0) of item being currently rendered with special values for header (-1) and footer (-2).
To better illustrate RepeaterX's operation lets consider few simple examples given with their outputs
<php:RepeaterX DataSource='${array("One", "Two", "Three", "Four")}'>
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>#${$this->Index}</td>
<td>${$this->Item}</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</php:RepeaterX>
Outputs
<table>
<tr>
<td>#0</td>
<td>One</td>
</tr>
<tr>
<td>#1</td>
<td>Two</td>
</tr>
<tr>
<td>#2</td>
<td>Three</td>
</tr>
<tr>
<td>#3</td>
<td>Four</td>
</tr>
</table>
We can see that RepeaterX iterated over DataSource we supplied, applying ItemTemplate template to every value inside.
Within any of RepeaterX's templates can be nested other controls (and vice-versa, RepeaterX can be put inside any other control). Example to illustrate this (using greeting.phpx from one of previous examples like "x:hello"):
<php:RepeaterX DataSource='${array("John", "Thomas", "Marry", "Ivan")}'>
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>#${$this->Index}</td>
<td><x:hello Name="${$this->Item}"/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</php:RepeaterX>
Will output
<table>
<tr>
<td>#0</td>
<td>Hello <strong>John!</strong></td>
</tr>
<tr>
<td>#1</td>
<td>Hello <strong>Thomas!</strong></td>
</tr>
<tr>
<td>#2</td>
<td>Hello <strong>Marry!</strong></td>
</tr>
<tr>
<td>#3</td>
<td>Hello <strong>Ivan!</strong></td>
</tr>
</table>
Final example would be to generate simple comma-separated list of names with last name separated with "and", all using Repater. This can be easily achieved using SeparatorTemplate template:
<php:RepeaterX DataSource='${array("John", "Thomas", "Marry", "Ivan")}'>
<HeaderTemplate>
Hello <span>
</HeaderTemplate>
<ItemTemplate>${$this->Item}</ItemTemplate>
<SeparatorTemplate>${$this->Index != 3 ? ', ' : ' and '}</SeparatorTemplate>
<FooterTemplate>
</span>
</FooterTemplate>
</php:RepeaterX>
Will output
Hello <span>John, Thomas, Marry and Ivan</span>
| « Previous Reading forms, redirecting user | Next » Revision history |
