[Drupal] How do I turn on twig debug mode in Drupal-8?

| | 2 min read

Twig is a modern template engine for PHP. It is fast, flexible, and more secure. Twig is a template framework and is a direct replacement for PHP Template. There are lots of differences between PHP template and Twig.

Drupal 8 is developed with help of Symfony2 framework, and the template files in Drupal 8 are in twig format.Read on to find out more about twig and debugging of twig.

Twig has its own syntax and coding standards

How to do Debugging of twig in Drupal?

In Drupal 8 there is a mode for debugging twig. How to turn on the debugging mode?

  • find out sites/default/services.yml file
  • find out twig.config parameters in services.yml Set the debug variable to true. By default, it is FALSE.
    
          parameters:
            twig.config:
              debug: true
          

    when debugging was enabled then

    • The markup of each Twig template is global by HTML comments that contain theming information, such as template file name suggestions.
    • Note that this debugging markup will cause automated tests that directly check rendered HTML to fail. When running automated tests, 'debug' should be set to FALSE.
    • The dump() function is used in Twig templates to output information bout template variables.
    • Twig templates are automatically recompiled when the source code changes (see auto_reload below).

And also twig has automatic reloading (recompiling) of templates, and caching compiled templates in the file system. it also in sites/default/services.yml

  • Twig auto-reload
    
          parameters:
            twig.config:
              auto_reload: true
         
  • Automatically recompile Twig templates when the source code changes.If you don't give a value for auto_reload, it will take based on the value of debug.By default, it is FALSE.
  • Twig cache
    
          parameters:
            twig.config:
              cache: true
         
  • By default, Twig templates will be compiled and stored in the file system to increase performance. Disabling the Twig cache will recompile the templates from the source each time they are used. In most cases, the auto_reload setting above should be enabled and not disabling the Twig cache. By default it is true.

reference