Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal 8 Theming with Twig

By editor , 20 March, 2016
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') }}

Tags

  • Drupal 8
  • Twig
  • Theming

Comments

Plain text

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

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer