Skip to main content

Alright, picture this: you're knee-deep in Drupal 10 development, churning out code like a pro. But hold up, what's this? Twig debug mode is still on in production? Cue the headaches. Suddenly, your beautifully crafted HTML is drowning in unnecessary output, and innocent contact form responses are sprouting template suggestions left and right. It's a mess.

In the world of Drupal, keeping things secure and streamlined is key. And one crucial piece of the puzzle? Managing Twig template debugging. Get this wrong, and you're in for a world of performance woes and potential security snafus.

So, how do we wrangle this wild Twig debug beast? It all starts with a little config magic in services.local.yml.


Configuring Twig Debug in services.local.yml

Twig debugging is a lifesaver during development, offering up juicy template details and even live reloading for those rapid-fire changes. But here's the kicker: it's a big no-no in production. Trust me, you don't want it hogging resources or accidentally leaking sensitive info to the world.


Best Practices


1. Local Only Configuration

Always make sure to configure Twig debug settings within services.local.yml, not services.yml. This ensures debug settings stay far, far away from production environments. After all, services.yml is typically reserved for default or production-safe configurations.


2. Using settings.php for conditional loading

To level up your Drupal development game, add some conditional loading mojo to your settings.php. This nifty trick ensures that specific configurations meant for development don't accidentally mess with your production site.


Instructions for updating settings.php

Find your trusty settings.php file, usually hanging out in the sites/default directory of your Drupal installation. Then, tweak it to conditionally load services.local.yml.  Add the following code to your settings.php to check for the presence of services.local.yml before loading it. This setup prevents the file from being loaded in environments where it's not present, thus avoiding deployment errors.

   * Load services definition file.
  $settings['container_yamls'][] = $app_root . '/' . $site_path . '/services.yml';
  if (file_exists($app_root . '/' . $site_path . '/services.local.yml')) {
    $settings['container_yamls'][] = $app_root . '/' . $site_path . '/services.local.yml';


3. Avoid overriding production settings in services.yml

Whatever you do, keep your mitts off services.yml when it comes to debug settings. Trust me, you don't want to mess with stuff like this

    debug: false
    auto_reload: null
    cache: true


4. Proper configuration in services.local.yml

When you're in the local development zone, tweak those Twig settings in services.local.yml to turbocharge your debugging capabilities

     debug: true
     auto_reload: true
     cache: true


Now, let's dive into what each of these settings does and how it makes your dev life a whole lot easier:

Settings Purpose
debug: true

When Twig debugging is on, it wraps each Twig template's HTML output with handy comments. These comments spill the beans on theming info, like template file names and block names. It's like having a GPS for your Twig templates, guiding you through the tangled web of code.


auto_reload: true

This nifty setting automatically recompiles Twig templates whenever their source code changes. No more manual cache clearing or refreshing pages like a madman. It's like having a personal assistant, making sure your templates stay fresh to death with every code tweak.


cache: true

Even during development, keeping the Twig cache enabled means that templates are compiled once and stored. This means less time waiting for pages to load and more time building awesome features. It's like having a secret stash of pre-made templates, ready to dazzle your users at a moment's notice.

While it might seem counterintuitive in a development setting, maintaining the cache enabled while also using auto_reload strikes a balance between performance and convenience. It prevents the performance degradation that would occur if every template had to be recompiled on every page load, yet still ensures that changes are recognised and compiled as needed.



Benefits to Developers

This setup allows developers to maintain separate configurations for different environments (local, staging, production) without risking the accidental deployment of inappropriate settings.  By tweaking these settings just right, developers can keep their development environments humming along smoothly while avoiding pesky deployment mishaps:

Separate Configurations – keep your development and production environments neatly separated, preventing debug settings from sneaking into the wild.

Error-Free Deployment – with conditional loading in settings.php, you can breathe easy knowing that services.local.yml won't cause any deployment headaches.

Freedom to Experiment – go wild in your local environment, tweaking configurations to your heart's content without fear of wreaking havoc on your live site.

Through making these changes into your settings.php not only safeguards your production environment but also enhances the development workflow by clearly segregating development-specific settings from production configurations.


A couple of notes to keep in mind

Remember to Review Regularly

As your project evolves, take some time to review your configurations regularly. Make sure only the essentials are enabled and that new team members are up to speed on the setup.

Document Everything

Keep track of your configurations in a log or documentation. This helps maintain consistency across deployments and reduces the risk of configuration mishaps.


Security and performance considerations


Twig debug mode can be a double-edged sword, providing detailed error messages that could potentially be exploited. By keeping debug settings out of production, you're safeguarding your application from prying eyes.


Enabling Twig debugging can put a strain on performance, thanks to all that extra processing and output. By disabling it in production, you're ensuring your site runs like a well-oiled machine, even under heavy traffic.


Wrap up

Managing Twig settings might not be the most glamorous part of Drupal development, but it's essential for keeping your site running smoothly. By isolating development configurations to services.local.yml and keeping them out of production, you can maintain the integrity and performance of your Drupal site, one line of code at a time.


Related articles

Andrew Fletcher31 May 2024
Connecting AWS S3 with Docker for Drupal 10
Recently, I encountered an issue where my local Docker environment refused to connect to AWS S3, although everything worked seamlessly in AWS-managed environments. This challenge was not just a technical hurdle; it was a crucial bottleneck that needed resolution to ensure smooth Drupal deployments...