Template functions, includes and cache blocks
This page is for application creators who write the templates of their extension. The template language is described in 6. The template language in ten minutes; this page collects what applications use most.
Contents
- Variables
- Includes and template parts
- The view functions
- Loops and conditions
- Cache blocks
- Structuring an extension's templates
Variables
| Function | Use |
|---|---|
{def $a = ...} | declare (once per template) |
{set $a = ...} | change |
{undef $a} | free; undefine what you defined at the end of a template |
{set-block variable=$html}...{/set-block} | capture output into a variable |
Includes and template parts
{include uri='design:expdemo/parts/item_card.tpl' item=$item show_image=true()}Parameters become variables of the included template; it is found through the design chain, so sites can override it. Use parts for markup that repeats.
The view functions
| Function | Renders |
|---|---|
{node_view_gui content_node=$node view='line'} | a node with node/view/line.tpl or its override |
{content_view_gui content_object=$object view='embed'} | an object |
{attribute_view_gui attribute=$node.data_map.body} | an attribute with its datatype's template |
{attribute_edit_gui attribute=$attribute} | an attribute's edit field |
Loops and conditions
{foreach $items as $index => $item}
{if $index|eq( 0 )}<h2>{$item.title|wash}</h2>{else}<p>{$item.title|wash}</p>{/if}
{delimiter}<hr>{/delimiter}
{/foreach}{foreach $list as $item max 5 offset 2} limits the loop; {break} and {continue} work as in PHP.
Cache blocks
{cache-block keys=array( $item.id, $current_user.contentobject_id ) expiry=600}
... expensive output ...
{/cache-block}| Parameter | Meaning |
|---|---|
keys | what the output depends on |
expiry | seconds; the default is 7200 (two hours, eZTemplateCacheFunction::DEFAULT_TTL); 0 never expires by time |
subtree_expiry | cleared when content below that node or address is published |
ignore_content_expiry | not cleared by publishing |
Data from your own tables is not tied to publishing: set an expiry that matches how stale it may be, or clear the
template-block cache when your data changes (Caching in applications).
Structuring an extension's templates
extension/expdemo/design/standard/templates/expdemo/
├── list.tpl one per view
├── item.tpl
└── parts/item_card.tpl reusable parts- One template per view, named after it.
- Parts in
parts/; override templates (per class) inoverride/templates/. - No inline
<script>or<style>, nostyle="": CSS and JS go throughdesign.inilists (Stylesheets and scripts). |washon every printed value;|i18n( 'extension/expdemo' )on every text.- Check each with
php bin/php/eztemplatecheck.php.
Related pages
- View results, templates and the pagelayout
- Template function pattern
- View cache and cache blocks
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.