Skip to the content
Documentation

Basic template tasks

No ratings yet

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 &lt; 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

Contributors

Comments (5 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. I'm vegetarian

    Matthew ·

    Could I suggest...

    {def $my_string='noodle'}
    {concat( 'Tempeh ', $my_string, ' stir-fry' )}

    As an alternative menu item?

  2. 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..

  3. view_parameters inside node/view/line.tpl template

    daniele nocentini ·

    I can't retrive view_parameters under the template node/view/line.tpl.

    1. Re: view_parameters inside node/view/line.tpl template

      André R. ·

      Try using the 'global scope' hash, like this:

      {$#view_parameters.color}

Comments

Add a comment

Comments are read by a moderator before they appear.