Twig is a template engine for PHP, which is the language used in Drupal. Twig can be used for templates in generic PHP applications as well as Drupal themes. The Drupal implementation of Twig is the same as regular Twig with a couple of Drupal additions. Drupal adds some custom filters and automatically handles the render pipeline for you. Anything you can do in regular Twig you can also do in Drupal's implementation of Twig.
- Customize markup in templates for a Drupal site
- Use dynamic data from variables within template files
- Customize markup in PHP applications that use Twig for templates
Resources on this page use both generic PHP applications as well as template files from Drupal 8 themes for context and examples. The Twig that is used in generic PHP applications can be used in Drupal templates as well. Note that Drupal has extended Twig and provides some Drupal-specific filters and functions. In some cases, these Drupal-specific functions or filters are preferable to their generic Twig counterparts.
Note: Drupal 8 uses Twig version 1.x and Drupal 9 uses Twig version 2.x. Be sure to use the appropriate version when consulting Twig's official documentation.
attach_library in Attach a Library.