Basic template tasks
This section collects tasks that come up in almost every template: including another template, building links, escaping output, joining strings, translating texts and reading parameters from the address. Links, images and design files are on their own page, URL handling.
Including a template
{include} renders another template in place. The prefix design: looks the file up in the design chain, below templates/, with the override rules for it:
{include uri='design:parts/footer.tpl'}
{include uri='design:parts/teaser.tpl' item=$child show_image=true()}
Every other parameter becomes a variable of the included template ($item, $show_image). The included template also sees the variables of the template that includes it; the variables it defines itself end with it. A parameter the caller may leave out gets a default with {default}:
{* parts/teaser.tpl *}
{default show_image=false()}
<h3>{$item.name|wash}</h3>
{if $show_image}{attribute_view_gui attribute=$item.data_map.image image_class='small'}{/if}
{/default}
An included template can be a Smarty or Twig template too: {include uri='design:parts/menu.twig'} (see Template engines). Details: include.
Escaping output
Print every value that comes from content, from the address or from a visitor through wash, which escapes it for HTML:
{def $text = 'hello < world'}
{$text|wash} {* hello < world *}
{undef $text}
The other modes of wash and when not to use it: Template security and wash.
Joining strings
Strings do not expand variables, so text and values are joined with concat, or with append and prepend in a pipe:
{def $filling = 'sausage'}
{concat( 'Liver ', $filling, ' sandwich' )} {* Liver sausage sandwich *}
{undef $filling}
A longer piece of output can be rendered into a variable with {set-block}:
{set-block variable=greeting}Hello {$current_user.contentobject.name|wash}{/set-block}
<p>{$greeting}</p>
Translatable texts
Text of the design goes through i18n with a context, so that it can be translated in the translation files of the design or extension:
{'Search'|i18n( 'design/standard/layout' )}
{'%count results'|i18n( 'design/mydesign/search', , hash( '%count', $count ) )}
View parameters
The address of a node can carry parameters at its end, each as (name)/value:
https://www.example.com/company/about-us/(color)/green/(amount)/34 https://www.example.com/content/view/full/13/(color)/green/(amount)/34
The node template receives them in $view_parameters, as strings, next to the built-in parameters offset, year, month, day and namefilter:
{if is_set( $view_parameters.color )}
The color is {$view_parameters.color|wash}.
{/if}
{def $offset = $view_parameters.offset}
View parameters are part of the address, so they are part of the content view cache key: each combination is cached on its own. They come from the visitor, so always wash them, and never pass them unchecked to a fetch. Custom view parameters are allowed by template.ini [ControlSettings] AllowUserVariables=true, the default. A paging list reads its offset the same way; see Information extraction. Everything about them: View parameters.
In this section
- URL handling Links, design images and design files in Exponential 6 templates: the operators ezurl, ezimage, ezdesign and ezroot.
- Custom view parameters / Basic template tasks / Templates... Exponential Documentation
- view_parameters inside node/view/line.tpl template / Basi... Exponential Documentation
Contributors
- Balazs Halasy
- Geir Arne Waaler
- Matthew Commenter
- meinhard benn Commenter
- André Rømcke
- daniele nocentini Commenter
- Graham Brookins Updated Maintainer
Comments (5 historical)
Written by readers of the earlier documentation sites, kept as they were.
I'm vegetarian
Matthew ·
Could I suggest...
{def $my_string='noodle'} {concat( 'Tempeh ', $my_string, ' stir-fry' )}As an alternative menu item?
Custom view parameters
meinhard benn ·
In some cases $view_parameters won't work, try $module_result.view_parameters there. So the example above will be:
The color is: {$module_result.view_parameters.color} <br /> The amount is: {$module_result.view_parameters.amount} <br />I hope this saves some headache..
Re: Custom view parameters
André R. ·
Those 'cases' is in the pagelayout template and included templates, take a look here for all variables in pagelayout:
http://ez.no/doc/ez_publish/techn...e_pagelayout/variables_in_pagelayout
view_parameters inside node/view/line.tpl template
daniele nocentini ·
I can't retrive view_parameters under the template node/view/line.tpl.
Re: view_parameters inside node/view/line.tpl template
André R. ·
Try using the 'global scope' hash, like this:
{$#view_parameters.color}