Drupal 8 Theming with Twig | Drupal 8

Drupal 8 Theming with Twig

Submitted by editor on Sun, 03/20/2016 - 11:17
Question

How to theme drupal 8 site using Twig theming system

Print : {{ variable_to_print }}
Code : {% codes %}
Comment : {# Comments #}
 

Official guides:

https://www.drupal.org/theme-guide/8/twig
https://www.drupal.org/theme-guide/8

Twig template guide : http://twig.sensiolabs.org/doc/templates.html

 

 

Twig Functions And Filters

Filters (https://www.drupal.org/node/2357633)

t - t() - {{ 'Home'|t }}
placeholder - drupal_placeholder() - {% trans %}Submitted on {{ date|placeholder }}{% endtrans %}
drupal_escape - - 
safe_join - - {{ items|safe_join(', ') }}
without - - {{ content|without('links') }}
clean_class - Html::getClass() - 
clean_id - Html::getID() - 
format_date - DateFormatter::format() - 
/!\ Avoid raw filter - {{ var1|raw }}

Functions  (https://www.drupal.org/node/2486991)

url($name, $parameters, $options)
<a href="{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>

url($name)
{{ path('<front>') }}

link($text, $url, $attributes)
{{ link(item.title, item.url, { 'class':['foo', 'bar', 'baz']} ) }}

file_url($uri)
{{ file_url(node.field_example_image.entity.uri.value) }}

attach_library($library)
{{ attach_library('classy/node') }}

 

Variables

Example equivalant
{{ foo.bar }}
{{ foo['bar'] }}
​{{ attribute(foo, 'bar') }}

{# equivalent to the non-working foo.data-foo #}
{{ attribute(foo, 'data-foo') }}

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.