Skip to main content

Disabling Twig's Template Cache in Drupal 8

Tags

Drupal 8 and Twig

Lately I have been working with Drupal 8 trying to get my head around what it takes to create a production application in the latest version of Drupal.  One of the very first snags I hit was theming.  Working with the theme system in Drupal 8 is quite a bit different from Drupal 7 beings how Drupal 8 utilizes Twig as the theme engine instead of PHPTemplate.  Drupal 8 was my first exposure to Twig so diving right into development came with a bit of a learning curve to get started.  I was under the impression that I could start altering the theme templates right away and I could see the results when the page was reloaded.  No dice!  To remedy this I figured I could turn off site caching and I would be able to see my template changes immediately.  Still, no dice!  At this point I knew that there must be more going on under the hood so I figured I better go back and read the documentation on how Twig works within Drupal to get a better understanding of what is going on.  It turns out that Twig templates are compiled into PHP code and the output is stored in memory for added performance boosts.  Clearing the Drupal site cache will wipe out these compiled templates and you will be able to see your template changes, but for continuous theme development, this is not an optimal workflow.  So, how can you disable Twig's template cache?  In further reading of the documentation I found that the Twig's caching settings are available in the site's services.yml file located at /sites/default/services.yml.  In altering these settings you will be able to see your template changes every time you reload the page without having to flush the Drupal site cache. 

Before getting started it is important to remember that Twig's template caching is something that needs to be left intact in a staging or production environment to take advantage of the performance boosts that it gives you.  Disabling Twig's template cache should only be done in a local development environment while theming your Drupal site. 
 

In your local development environment find your settings.php file, found at /sites/default/settings.php.  If you have a default.settings.php file, copy this file and rename it settings.php in the /sites/default directory.  Make sure your settings.php file has a reference to the services.yml file and the settings.local.php file.  If you do not have a settings.local.php file, take the example.settings.local.php file from /sites and copy it to /site/default/settings.local.php.

<?php
 ...
 $settings['container_yamls'][] = __DIR__ . '/services.yml';
 
/**
 * If there is a local settings file, then include it for local development setup
 */
 $local_settings = __DIR__ . "/settings.local.php";
 if (file_exists($local_settings)) {
   include $local_settings;
 }
 $settings['install_profile'] = 'standard';

Next take a look at the /sites/default/services.yml.  Make sure the twig.config settings match the following below.  This will tell Drupal to recompile the Twig templates on every page request and not to cache the output.
 

parameters:
 ...
  twig.config:
    # Twig debugging:
    #
    # When debugging is enabled:
    # - The markup of each Twig template is surrounded 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 can be used in Twig templates to output information
    #   about template variables.
    # - Twig templates are automatically recompiled whenever the source code
    #   changes (see auto_reload below).
    #
    # For more information about debugging Twig templates, see
    # https://www.drupal.org/node/1906392.
    #
    # Not recommended in production environments
    # @default false
    debug: true
    # Twig auto-reload:
    #
    # Automatically recompile Twig templates whenever the source code changes.
    # If you don't provide a value for auto_reload, it will be determined
    # based on the value of debug.
    #
    # Not recommended in production environments
    # @default null
    auto_reload: true
    # Twig cache:
    #
    # By default, Twig templates will be compiled and stored in the filesystem
    # to increase performance. Disabling the Twig cache will recompile the
    # templates from source each time they are used. In most cases the
    # auto_reload setting above should be enabled rather than disabling the
    # Twig cache.
    #
    # Not recommended in production environments
    # @default true
    cache: false
 ...

Next the settings.local.php needs to be altered to disable the render cache.  Find the two settings in settings.local.php and uncomment them.

<?php
 ...
 /**
  * Enable local development services.
  */
 $settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
 ...
 /**
  * Disable the render cache (this includes the page cache).
  *
  * Note: you should test with the render cache enabled, to ensure the correct
  * cacheability metadata is present. However, in the early stages of
  * development, you may want to disable it.
  *
  * This setting disables the render cache by using the Null cache back-end
  * defined by the development.services.yml file above.
  *
  * Do not use this setting until after the site is installed.
  */
 $settings['cache']['bins']['render'] = 'cache.backend.null';
 
 /**
  * Disable Dynamic Page Cache.
  *
  * Note: you should test with Dynamic Page Cache enabled, to ensure the correct
  * cacheability metadata is present (and hence the expected behavior). However,
  * in the early stages of development, you may want to disable it.
  */
 $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Lastly, make sure the file development.services.yml is located in /sites/default/development.services.yml.  Above you may have see that this file was referenced in the settings.local.php.  This file activates the cache.backend.null setting that was uncommented in settings.local.php.

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Member for

3 years 9 months
Matt Eaton

Long time mobile team lead with a love for network engineering, security, IoT, oss, writing, wireless, and mobile.  Avid runner and determined health nut living in the greater Chicagoland area.