WDTUTORIALS

Drupal 8 - How To Use Twig Tweak Module

Tutorial on how to add useful filters and functions to templates .

How to install it?

drush -y en twig_tweak

Print views in templates

{{ drupal_view('latest_posts', 'default') }}

You can use arguments:

{{ drupal_view('latest_posts', 'default', '81,60') }}
Name Description
latest_posts View machine name.
default Display ID (default, block_1 etc).
81,60 (optional) Arguments. Example: add an ID contextual filter to the view and you can limit the view results for only selected nodes (81 and 60 in this case).

This function uses Views Embed View function that doesn’t display the title of the view. You can print it manually above the view or use the drupal_block function below (if you don’t use arguments).

Print blocks in templates

{{ drupal_block('mytheme_search') }}
Name Description
mytheme_search The machine name of the block.

Print tokens in templates

{{ drupal_token('site:name') }}

Example tokens:

Name Description
site:name Site name.
site:slogan Site slogan.
current-date:long Current date in long format

You can install Token module to see all available tokens in here: admin/help/token.

Use token_replace filter to print out multiple tokens at once:

{{ '[site:name] [current-date:long]' | token_replace | raw }}
Name Description
[site:name][current-date:long] The tokens.
token_replace The filter.
raw I had to use this filter for the HTML tags to show up right.

You can also access configuration data with drupal_config:

{{ drupal_config('system.site', 'name') }}

PHP filter

{{ 'return date("Y d m");' | php }}

Allowing PHP like this in templates can create security holes and perfomance issues.
Read more about the PHP Filter module:
- https://www.drupal.org/project/php
- https://www.drupal.org/documentation/modules/php
- https://www.drupal.org/node/1203886

Search and replace with regular expressions

{{ 'some text' | preg_replace('some', 'other') | raw }}

Dis replaces the word some with other.

Name Description
some text The text to apply search and replace.
some The text to look for.
other the replacement.
raw I had to use raw filter to make it work.

Render entities

At this moment (2016.08.24) with the stable version: 8.x-1.2 this function seemed to get stuck in a loading screen.

This will render the whole node:

{{ drupal_entity('node', 1) }}

This will render the teaser:

{{ drupal_entity('node', 1, 'teaser') }}

This will render the current node:

{{ drupal_entity('node', NULL, 'teaser') }}
Name Description
node The entity type.
1 Entity ID.
teaser The view mode.

In the video I also use Bootstrap to divide node content into two columns and show Recent Posts view next to the body field.

Sources & links:

Samuli Natri (Entrepreneur, WDTutorials.com founder) has been building websites since the late 90's. He attended Helsinki University Of Technology (Computer Science) and Helsinki University (Social Sciences).

Latest Tutorial CKEditor Tutorial With CodeSnippet Syntax Highlighting

Copyright @ 2018 WDTutorials.com. All rights reserved. Privacy policy.Terms Of Service.