Enable Debugging in Twig Templates - Drupal 8





5.00/5 (2 votes)
How to enable debugging in Twig templates - Drupal 8
Introduction
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:
- Navigate to directory /sites/default.
- Make a copy of default.services.yml file and rename it to services.yml.
- Open the services.yml file and locate '
twig.config
'. - Set
debug
totrue
.(OPTIONAL: Set
auto_reload
totrue
andcache
tofalse
)parameters: twig.config: debug: true auto_reload: true cache: false
- 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:
- Navigate to directory /sites.
- Make a copy of example.settings.local.php file, rename to settings.local.php & place in the directory /sites/default.
- 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\NullBackendFactory
- Navigate to directory /sites/default.
- 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'; }
- Clear the cache.
History
- 2nd October, 2019: Initial version