To apply a template to any part of your webpage, you need three (optionally four) elements:
- An entry in the configuration that defines which template should be used in what situation
- The template file itself
- Assets used by the template (for example, CSS or JS files, images, etc.)
- Optional: A custom controller used when the template is read which allows you more detailed control over the page.
Each template must be mentioned in a configuration file together with a definition of the situation in which it is used. You can use the
ezplatform.yml file located in the
app/config/ folder, or create your own separate configuration file in that folder that will list all your templates.
If you decide to create a new configuration file, you will need to import it by including an import statement in
ezplatform.yml. Add the following code at the beginning of
If you are using the recommended .yml files for configuration, here are the basic rules for this format:
- The configuration is based on pairs of a key and its value, separated by a colon, presented in the following form:
- The value of the key may contain further keys, with their values containing further keys, and so on.
- This hierarchy is marked using indentation – each level lower in the hierarchy must be indented in comparison with its parent.
A short configuration file can look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
This is what individual keys in the configuration mean:
systemare obligatory at the start of any configuration file which defines views.
defaultdefines the SiteAccess for which the configuration will be used. "default", as the name suggests, determines what views are used when no other configuration is chosen. You can also have separate keys defining views for other SiteAccesses.
layoutpoint to the main template file that is used in any situation where no other template is defined. All other templates extend this one.
content_viewdefines the view provider.
In earlier version
location_view was used as the view provider. It has been deprecated since eZ Platform 1.x.
linedetermine the kind of view to be used (see below).
blog_postare the keys that start the configuration for one individual case of using a template. You can name these keys any way you want, and you can have as many of them as you need.
templatenames the template to be used in this case, including the folder it is stored in (starting from
controllerdefines the controller to be used in this case. Optional, if this key is absent, the default controller is used.
matchdefines the situation in which the template will be used. There are different criteria which can be used to "match" a template to a situation, for example a Content Type, a specific Location ID, Section, etc. You can view the full list of matchers here: View provider configuration. You can specify more than one matcher for any template; the matchers will be linked with an AND operator.
In the example above, three different templates are mentioned, two to be used in full view, and one in line view. Notice that two separate templates are defined for the "article" Content Type. They use the same matcher, but will be used in different situations – one when an Article is displayed in full view, and one in line view. Their templates are located in different folders. The line template will also make use of a custom controller, while the remaining cases will employ the default one.
Full, line and other views¶
Each Content item can be rendered differently, using different templates, depending on the type of view it is displayed in. The default, built-in views are:
- full (used when the Content item is displayed by itself, as a full page)
- line (used when it is displayed as an item in the list, for example a listing of contents of a folder)
- embed (used when one Content item is embedded in another, as a block)
- embed-inline (used when a Content item is embedded inline in another block)
Other, custom view types can be created, user for example for embedding one Content item in another, but only these four have built-in controllers in the system.
For more details, see View provider configuration.
Templates in eZ Platform are written in the Twig templating language.
Twig templates in short
At its core, a Twig template is an HTML frame of the page that will be displayed. Inside this frame you define places (and manners) in which different parts of your Content items will be displayed (rendered).
Most of a Twig template file can look like an ordinary HTML file. This is also where you can define places where Content items or their fields will be embedded.
The configuration described above lets you select one template to be used in a given situation, but this does not mean you are limited to only one template file per case. It is possible to include other templates in the main template file. For example, you can have a single template for the footer of a page and include it in many other templates. Such templates do not need to be mentioned in the configuration .yml file.
See Including Templates in Symfony documentation for more information on including templates.
The main template for your webpage is placed in a pagelayout.
You can define the pagelayout per SiteAccess using the
This template will be used by default for those parts of the website where no other templates are defined.
pagelayout.html.twig file exists already in Demo Bundles, but if you are using a clean installation, you need to create it from scratch. This file is typically located in a bundle, for example using the built-in AppBundle:
src/AppBundle/Resources/views. The name of the bundle must the added whenever the file is called, like in the example below.
Any further templates will extend and modify this one, so they need to start with a line like this:
Although using AppBundle is recommended, you could also place the template files directly in
<installation_folder>/app/Resources/views. Then the files could be referenced in code without any prefix.
In short, the
Resources/views part of the path is automatically added whenever a template file is referenced. What you need to provide is the bundle name, name of any subfolder within
/views/, and file name, all three separated by colons (:)
To find out more about the way of referencing template files placed in bundles, see Referencing Templates in a Bundle in Symfony documentation.
Templates can be extended using a Twig
block tag. This tag lets you define a named section in the template that will be filled in by the child template. For example, you can define a "title" block in the main template. Any child template that extends it can also contain a "title" block. In this case the contents of the block from the child template will be placed inside this block in the parent template (and override what was inside this block):
1 2 3 4 5 6 7
1 2 3 4 5
In the simplified example above, when the
child.html.twig template is used, the "title" block from it will be placed in and will override the "title" block from the main template – so "Specific title" will be displayed instead of "Default title."
Alternatively, you can place templates inside one another using the
See http://twig.sensiolabs.org/doc/templates.html# for detailed documentation on how to use Twig.
Embed content in templates¶
Now that you know how to create a general layout with Twig templates, let's take a look at the ways in which you can render content inside them.
There are several ways of placing Content items or their Fields inside a template. You can do it using one of the Twig functions described in detail here.
As an example, let's look at one of those functions: ez_render_field. It renders one selected Field of the Content item. In its simplest form this function can look like this:
This renders the value of the Field with identifier "description" of the current Content item (signified by "content"). You can additionally choose a special template to be used for this particular Field:
1 2 3 4 5
As you can see in the case above, templates can be created not only for whole pages, but also for individual Fields.
Another way of embedding Content items is using the
render_esi function (which is not an eZ-specific function, but a Symfony standard). This function lets you easily select a different Content item and embed it in the current page. This can be used, for instance, if you want to list the children of a Content item in its parent.
This example renders the Content item with Location ID 33 using the line view. To do this, the function applies the
ez_content:viewAction controller. This is the default controller for rendering content, but can be substituted here with any custom controller of your choice.
Asset files such as CSS stylesheets, JS scripts or image files can be defined in the templates and need to be included in the directory structure in the same way as with any other web project. Assets are placed in the
web/ folder in your installation.
Instead of linking to stylesheets or embedding images like usually, you can use the
While it is possible to template a whole website using only Twig, a custom PHP controller gives many more options of customizing the behavior of the pages.
See Custom rendering logic for more information.
Rendering Content items¶
By default (without any configuration), a Content item is rendered without any template. By creating multiple templates and configuring them properly, you can configure the platform to render Content items differently depending on the scenario.
Content item Fields¶
A view template receives the requested Content item, holding all Fields. In order to display the Fields' value the way you want, you can either manipulate the Field Value object itself, or use a custom template.
Getting raw Field value¶
As you have access to the Content item in the template, you can use its public methods to access all the information you need. You can also use the
ez_field_value helper to get the Field's value only. It will return the correct language if there are several, based on language priorities.
1 2 3 4
Rendering Content items on full page¶
To render a Content item on a full page, first you need to create an
1 2 3 4
Next, you need to provide the template configuration.
You can place the config in the
app/config folder in either of two places: a new configuration file or the pre-existing
In this case you'll use the latter.
ezplatform.yml, under the
system keys, add the following config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Pay attention to indentation –
default should be indented relative to
match to identify not only the Content Type, but also the scenario for using the template.
For details, see Matchers.
At this point all Content items that are articles should render using the new template.
If you do not see changes, clear the cache by running:
php bin/console cache:clear.
Using the Field Type's template block¶
All built-in Field Types come with their own Twig template.
You can render any Field using this default template using the
You can use this helper to render various Content item fields. This, paired with the fact that each Content item can have multiple fields and you can render them differently, offers more rendering options.
To see it in practice, extend the
1 2 3 4 5 6 7 8
For more details on the
ez_render_field() helper, see Twig functions reference guide.
As this makes use of reusable templates, using
ez_render_field() is the recommended way and is to be considered the best practice.
The name of a Content item is its generic "title", generated by the repository based on the Content Type's naming pattern. It often takes the form of a normalized value of the first field, but might be a concatenation of several fields. There are 2 different ways to access this special property:
- Through the name property of ContentInfo (not translated).
- Through VersionInfo with the TranslationHelper (translated).
The translated name is held in a
VersionInfo object, in the
names property which consists of a hash indexed by locale. You can easily retrieve it in the right language via the
1 2 3 4 5
The helper will by default follow the prioritized languages order. If there is no translation for your prioritized languages, the helper will always return the name in the main language.
You can also force a locale in a second argument:
Name property in ContentInfo
This property is the actual Content name, but in the main language only (so it is not translated).
In PHP that would be:
So make sure to use
$content->getName() or $versionInfo->getName(), which takes translations into account.
The Rich Text Field allows you to embed other Content items within the Field.
Content items that are identified as images will be rendered in the Rich Text Field using a dedicated template.
You can determine which Content Types will be treated as images and rendered using this template in the
ezplatform.content_view.image_embed_content_types_identifiers parameter. By default it is set to cover the Image Content Type, but you can add other types that you want to be treated as images, for example:
The template that is used when rendering embedded images can be set in the
ezplatform.default_view_templates.content.embed_image container parameter:
Links to other Locations¶
Linking to other Locations is done with a native
path() Twig helper (or
url() if you want to generate absolute URLs). When you pass it the Location object,
path() will generate the URLAlias.
If you don't have the Location object, but only its ID, you can generate the URL alias the following way:
Instead of pointing to a specific Content item by its Location ID, you can also use here a variable. Fore more details, see this example in the Demo Bundle.
You can also use the Content ID. In that case the generated link will point to the Content item's main Location.
Under the hood
In the back end,
path() uses the Router to generate links.
This makes it also easy to generate links from PHP, via the
See also: Cross-SiteAccess links
Embedding Content items¶
To render an embedded Content from a Twig template you need to do a subrequest with the
This controller is exactly the same as the ViewController presented above. It has one main
viewAction that renders a Content item.
You can use this controller from templates with the following syntax:
The example above renders the Content item whose ID is 123 with the view type line.
ez_content controller follows the syntax of controllers as a service, as explained in Symfony documentation.
As with any controller, you can pass arguments to
ez_content:viewAction to fit your needs.
You must provide
contentId (and, optionally,
locationId) for the action to work.
||ID of the Content item you want to render. Can be used together with
||integer||Location's Content item, if defined|
||ID of the Location you want to render. Can be used together with
||integer||Content item's main location, if defined|
||The view type you want to render your Content item/Location in. Will be used by the ViewManager to select a corresponding template, according to defined rules. Example: full, line, my_custom_view, etc.||string||full|
||Indicates if the sub-view needs to use the main layout (see available variables in a view template)||boolean||false|
||Hash of variables you want to inject to sub-template, key being the exposed variable name.||hash||empty hash|
1 2 3 4 5 6 7 8 9 10
Listing Content item children¶
For details on listing children of a Content item, for example all content contained in a folder, see Displaying children of a Content item.
Rendering and cache¶
Just like for regular Symfony controllers, you can take advantage of ESI and use different cache levels:
Only scalar variables (not objects) can be sent via
Rendering in preview¶
When previewing content in the back office, the draft view is rendered using the PreviewController.
The first draft of a yet unpublished Content item does not have a Location, because Locations are only assigned when content is published. To enable rendering in such cases, the PreviewController creates a temporary virtual Location. This Location has some of the properties of the future Location, such as the parent Location ID. However, it does not fully replace a normal Location.
If the rendering template refers directly to the Location ID of the content, an error will occur.
To avoid such situations, you can check if the Location is virtual using the
location.isDraft flag in Twig templates, for example:
1 2 3
Exposing additional variables¶
You can dynamically inject variables in content view templates by listening to the
The event listener method receives an
The following example injects
my_array variables in all content view templates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8