Lots of people think that template engines like Twig cannot be interactively debugged. I heard this several times as an argument against template engine, and for using legacy php processing like phptemplate (standard in Drupal 7).
Well, it's not entirely true. And even more, debugging Twig is not only easy, but you will also get much more information about what your template is doing!
Are you not sure if you need to escape output string in template or if it's already escaped 'out of the box?' No worries: debugger tells you quickly, because you can debug the PHP scripts Twigs are compiled into (and see exactly what the script is doing with your variables).
A precondition for this how-to is completely performed in the previous episode of this series, Debugging Drupal 8 in PhpStorm. Part 2: Local Web-based Debugging in Mac OS X, Acquia Dev Desktop 2, and XDebug. This is where we describe how to set-up standard web-based debugging. In this article we will only describe some site-specific settings you must perform in Drupal 8 in order to debug PHP code generated by Twig templates.
It really makes sense to enable the settings below only for local environment. So firstly, we will setup the specific environment for local development only. If you have this done before, you can skip this step.
In your local sites/default/settings.php, ensure that the following piece of code is uncommented or added at the end of the file:if (file_exists(__DIR__ . '/settings.local.php')) { include __DIR__ . '/settings.local.php';}
Ensure file sites/default/settings.local.php
exists and following line is present there:$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
You will also need to disable render cache by uncommenting following line in sites/default/settings.local.php
file:$settings['cache']['bins']['render'] = 'cache.backend.null';
development.services.yml
Open /sites/development.services.yml
and ensure the following is present in the file:services: cache.backend.null: class: Drupal\Core\Cache\NullBackendFactoryparameters: twig.config: debug: true auto_reload: true cache: true
Let's explain this step a little: in fact, to enable Twig debugging, we only need to set 'cache: true'. This assures that the Twig templates will be 'cached' and generated into PHP files. And these PHP files are debuggable. In order to re-generate these PHP files after each page-load, we must disable caching by setting dummy 'NullBackendFactory' service as a caching service, and by setting 'auto_reload: true' as a Twig setting, so Twig will compile template files after each page load.
The 'debug: true' setting is actually not necessary to be set, but it's still very helpful. It will require a lot of HTML comments with information about the Twig file and the theme file name suggestions available.
In this folder, you see all the generated PHP files from Twig templates. Locate a folder with a name that contains name of your Twig template you want to debug (e.g. '..._breadcrumb.html.twig_').
Open the PHP file with a long hashed filename inside of this folder and set the breakpoint at first line of the doDisplay()
function:
Note: if you see more than one php file in this folder, set a breakpoint to all php files at the same place inside doDisplay function - see last bullet below for closer explanation why.
Refresh the page, and PhpStorm should be stopped at the position inside the doDisplay function. Voila! :)
Some additional useful information:
breadcrumb.html.twig
, the 'breadcrumb' variable assigned to the template is shown under $context[âbreadcrumb']
:doDisplay()
function. Then you should surely match one. If this content did not answer your questions, try searching or contacting our support team for further assistance.
Fri Sep 12 2025 06:51:44 GMT+0000 (Coordinated Universal Time)