Twig in Drupal 8

Twig is a template engine for PHP and it is part of the Symfony2 framework.

In Drupal 8 Twig replaces PHPTemplate as the default templating engine. One of the results of this change is that all of the theme_* functions and PHPTemplate based *.tpl.php files have been replaced by *.html.twig template files.

Note that Drupal 8 currently uses Twig version 1.x. A future release of Drupal 8 might use or support Twig 2.x (issue here). Some Twig features differ between these versions, so theme developers should avoid deprecated Twig features for future-compatible themes.

Working With Twig Templates

Drupal allows you to override all of the templates that are used to produce HTML markup so that you can fully control the markup that is

Twig Template naming conventions

Drupal loads templates based on certain naming conventions. This allows you to override templates by adding them to your theme and giving

Comparison of PHPTemplate and Twig theming paradigms

This page compares PHPTemplate to Twig.

Discovering and Inspecting Variables in Twig Templates

When working with a Twig template file most variables are documented in the comments for the template file. However, when they are not, or

Debugging Twig templates

The Twig templating engine offers a debug tool.

Debugging compiled Twig templates

The Twig engine provides options for configuring debugging, automatic reloading (recompiling) of templates, and caching compiled templates

Locating Template Files with Debugging

To know which template is generating the markup for a particular element, you can use Twig's built in debug option. This option

Filters - Modifying Variables In Twig Templates

Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments

Functions - In Twig Templates

Twig provides a number of handy functions that can be used directly within Templates.

Macros in Twig Templates

From the official Twig documentation: "Macros are comparable with functions in regular programming languages. They are useful to put often

Twig best practices - preprocess functions and templates

In order to make Drupal 8 theming as performant as possible and allow for more customization in Twig templates, please follow these best

Create custom twig templates from custom module

The general idea in Drupal 8 is that you want to avoid creating html directly in the PHP code of your custom module. You want this to go

Guide maintainers