Drupal 8 Theming Basics
Drupal 8 has changed its theme engine from phptemplate
to twig
which is part a of symphony2 framework.
Now twig templates that were defined as *.html.twig
.
To debug a twig template and print variables you need to enable debug : true
in twig.config:
, which is present
in the services.yml
file in sites->default folder.
Then we can use the devel
module along with devel kint
, after enabling this we only need to
call kint(varibale_name)
in the template.for example:
{{ kint(fields) }}
To print a varible in twig we use
`{{ }}`
. The variable is to be placed inside these curly braces. like
`{{ fields.field_name.content }}`
.
To write a IF condition
we use,
`{% if(fields.field_name.content) is not empty %}`
this is accompanied by
`{% endif %}`
, similarly .
Naming views templates:
Surprisingly drupal 8 views has taken the option to add templates and see what templates are being applied on a view by clicking on information
option in a view.
To override views templates we need to see what templates we need and where to find them.
We can find these in our core->modules->views->templates
.
Now copy the desired template and put it inside the templates folder in your custom theme or the theme you are using. Views use templates as:
Naming Custom templates: A template can be named by using simple rules:
these can be changed upon requirement as, we might need to add field name is we are adding template of forth style views-view-field.html.twig
.
For example if we have a view name: benefit_detail
and display is block_1
and we want add template of third type views-view-fields.html.twig
for this specific view, we will add
views-view-fields--benefit_detail--block_1.html.twig
.
-
TAGS:
- Drupal8
- Drupal
- learning