In this topic:
Table of Contents | ||
---|---|---|
|
Introduction
This page explains the basics of using templates in eZ CMS.
Tip |
---|
For more in-depth topics related to templating, see: Content view | View provider configuration | Default view templates | Twig functions | How to use a custom controller to display a content item or location |
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.
Configuration
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.
Note | ||
---|---|---|
If you decide to create a new configuration file, you will need to import it by including an import statement in
|
Tip |
---|
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: key: value. 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:
Section | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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.
Info |
---|
Full, line and other viewsEach 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), and embed (used when one Content item is embedded in another). Other, custom view types can be created, but only these three have built-in controllers in the system. |
Tip |
---|
See View provider configuration for more details. |
Template file
Templates in eZ CMS are written in the Twig templating language.
Info |
---|
Twig templates in shortAt 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 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.
Tip |
---|
See Including Templates in Symfony documentation for more information on including templates. |
Anchor | ||||
---|---|---|---|---|
|
pagelayout.html.twig
file. This template will be used by default for those parts of the website where no other templates are defined.A 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:
Code Block |
---|
{% extends "AppBundle::pagelayout.html.twig" %} |
Info |
---|
Although using AppBundle is recommended, you could also place the template files directly in |
Tip | ||
---|---|---|
| ||
In short, the 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):
Section | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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."
Tip |
---|
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:
Code Block |
---|
{{ ez_render_field( content, 'description' ) }} |
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:
Code Block |
---|
{{ ez_render_field( content, 'description', { 'template': 'AppBundle:fields:description.html.twig' } ) }} |
Note |
---|
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.
Code Block |
---|
{{ render_esi(controller('ez_content:viewAction', {locationId: 33, viewType: 'line'} )) }} |
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.
Assets
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 asset
function.
Controller
While it is absolutely 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 How to use a custom controller to display a content item or location for more information.