Enable Debugging in Twig Templates — Drupal 8

Vipin Mittal
2 min readFeb 19, 2020

Twig is a flexible, fast and secure template framework or modern engine of PHP which is integrated by the Drupal 8.

Twig Theme debugging helps the developers in developing new templates and manipulating existing templates effectively.

Enabling the twig debug mode on, the mark up of each twig template getting displayed in the HTML which can be clearly seen by viewing the page source or browser developer tool.

There are two similar options to enable the twig theme debugging.

Option 1

This is the simplest method. Follow the below steps sequentially to enable the twig template debugging by making changes in one file services.yml:

  1. Navigate to directory /sites/default.
  2. Make a copy of default.services.yml file and rename it to services.yml.
  3. Open the services.yml file and locate twig.config.
  4. Set debug to true.

(OPTIONAL: Set auto_reload to true and cache to false

parameters:
twig.config:
debug: true
auto_reload: true
cache: false

5. Clear the cache.

Option 2

Opt for this method in case of setting up different configuration as per specific environments. Follow the below steps sequentially to enable the twig template debugging:

  1. Navigate to directory /sites.
  2. Make a copy of example.settings.local.php file, rename to settings.local.php & place in the directory /sites/default.
  3. Open development.services.yml resided in same /sites directory and add code that the entire file will look like (mind indentation):
parameters:
http.response.debug_cacheability_headers: true
twig.config:
debug: true
auto_reload: true
cache: false
services:
cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory4.

4. Navigate to directory /sites/default.

5. Open settings.php and uncomment the following code:

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
include $app_root . '/' . $site_path . '/settings.local.php';
}

6. Clear the cache.

Kindly use either of the above options to enable the twig debugging.

--

--

Vipin Mittal

He is Passionate coder and Drupal enthusiast with 15 years core Drupal experience. He is Acquia Drupal certified and involved in different upgrades/migrations.