Logo Knowledge
  • Product Documentation
  • Insight
  • Developer Center
  • Training
  • Submit a Request
  • Product Documentation
  • Insight
  • Developer Center
  • Training
  • Submit a Request
  1. Acquia Support Knowledge Base
  2. Troubleshooting
  3. Common Problem

Debugging Twig

    See more
    • Updated
    • November 15, 2020 12:19

    Issue

    Tracing errors or issues according to the Twig template or variable in Drupal 8.

    Resolution

    There are two main ways of debugging Twig:

    • Enable Twig debugging which will help determine which templates are being used.
    • Add markup to the page that will allow us to explore the variables available in a template.

    Determining template suggestions

    Enabling Twig debugging will add markup to the page which tells you information about what templates are being used. The method to debug Twig is briefly outlined on Drupal.org at Debugging Twig templates, which uses a web browser's native debugging tools to aid in finding issues.

    Enable Twig debugging in /sites/default/services.yml file with the following:

    parameters: twig.config: debug: true 

    You then use developer tools with comments enabled to allow viewing information about the template. It may be difficult to find specific documentation on the best practices for using this method.

    Debugging Twig variables

    Twig supports a way to explore all of the variables available in a template as well as individual variables.

    To do this, install the Devel and Kint modules (Kint is included with Devel).

    You may need to install the Kint library via Composer: composer require raveren/kint

    Then dump all the variables to the screen with a command like this:

    {{ kint() }} 

    Or explore a single variable. Note in this example that you can see the Twig comment format {# comment #}:

    {# Or one variable. #} {{ kint(element) }} {# Or one part of the render array. #} {{ kint(element.hello) }} 

    This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

     

    Acquia Products

    • Acquia Cloud Site Factory

    External Links

    • Check out this article Demystifying Content Rendering
    Avatar
    Jacqi Jordan
    • November 15, 2020 12:19
    • Updated
    • Facebook
    • Twitter
    • LinkedIn

    Was this article helpful?
    2 out of 4 found this helpful

    Return to top

    Related articles

    • Drupal 8: Twig cache
    • Fast Track to Drupal 8 Coding
    • Best practices for Drupal permissions
    • Is there a Drupal function to check if the user is logged in?
    • Upgrading from PHP 7.2 to 7.3

    Support

    Acquia Support Knowledge Base
    • Submit a Request
    • Contact Support
    • Acquia Support Guide
    • Product Documentation
    • System Status

    About Acquia

    • About Us
    • Leadership
    • Board of directors
    • Newsroom
    • Careers
    • Customers
    • Contact Us
    53 State Street, 10th Floor
    Boston, MA 02109
    United States
    Phone: 888-922-7842
    Map: Google Maps
    View other locations
    • Feeds
    • Legal
    • Security Issue?

    Copyright © 2018 Acquia Inc. All Rights Reserved. Drupal is a registered trademark of Dries Buytaert.