I'm trying to dive into Drupal 8 Twig templating. My problem is that I need to clear Drupals caches every time I have made a change, even after disabling caches with Drupal Console ('drupal site:mode dev').

Comments

slewazimuth’s picture

If drupal console isn't working for twig settings you can manually do them in services.yml in the default directory of the sites directory under the twig.config group. To enable what is normally desired for development work the settings are typically set as

twig.config:

debug:true
auto_reload: true
cache: false

There are comments in the file which explain what each setting accomplishes. Always rebuild/clear cache after changing settings.

finngruwier’s picture

I just checked what combination of options Drupal Console actually sets in services.yml. It turns out that it sets cache to true! After setting cache to false (and clearing caches) it actually works.

I just assumed that 'drupal site:mode dev' would set cache to false which would have been the obvious choice. Thanks for helping with clearing this out.

sachinsuryavanshi’s picture

I have followed above steps
- twig.config:

debug:true
auto_reload: true
cache: false

and Making JS, CSS change too in performance tab.

Still I am not able to disable cache for my Twig template

altrugon’s picture

The settings proposed by slewazimuth may not be enough. Full instructions of how to disable cache for development can be found at Disable Drupal 8 caching during development

-------------------------------------------------
take a look -> www.altrugon.com

sachinsuryavanshi’s picture

I have added below line in my code and it worked for me.

\Drupal::service('page_cache_kill_switch')->trigger();
'#cache' => ['max-age' => 0,],

 public function dashboard(){
\Drupal::service('page_cache_kill_switch')->trigger();
$build['patient'] = [
		  '#theme' => 'patient-dashboard',
		  '#button_name' => $this->t('Enter your Daily Diary'),
		  '#pree_screen' => $this->t('PRE-SCEENING FORM'),
		  '#caregiver_screen' => $this->t('CAREGIVER REGISTARTION FORM'),
		  '#image_path' => $image_path,
		  '#dailyentry' => $dailyentry,
		  '#cache' => ['max-age' => 0,], 
		  '#uid' => $user->get('uid')->value, 
		  '#pre_screen_form' => $pre_screen_form, 
		  '#caregiver_flag' => $caregiver_flag, 
		];

return $build;
}
na.karimi’s picture

thank you

codesmith’s picture

The instructions on Disable Drupal 8 caching during development did the trick.

Rishi Kulshreshtha’s picture

thijsv’s picture

If you are using Drupal verison greater than or equal to 8.4 then add the following lines to your setting.local.php

$settings['cache']['bins']['page'] = 'cache.backend.null';
rahul_k’s picture

1.Copy and rename the file from sites/example.settings.local.php to  sites/default/settings.local.php
2.Open settings.php file in sites/default and uncomment these lines:

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
include $app_root . '/' . $site_path . '/settings.local.php';
}

3.Uncomment these lines in settings.local.php

$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

4. Open development.services.yml in the sites folder and add the following code

parameters:
twig.config:
debug: true
auto_reload: true
cache: false

4. Rebuild the cache and your good to go

Bruria Helfer’s picture

Worked like charm!! Thanks

hudri’s picture

Please remember that leading whitespaces are very important im .yml files. So the example parameters:-section from rahul_k's  development.services.yml file above should look like

parameters:
  twig.config:
    debug: true
    auto_reload: true
​​​​​​​    cache: false
smurfxx’s picture

I tried to disable the drupal and twig caches on Drupal 9 following your suggestions but it seems to not work, every time I change something to a twig template I must manually clear the cache.

Tried also to disable OPCache from PHP server but nothing changes.

Do you have any suggestions or faster methods to disable caches during development?

uri_frazier’s picture

The last step for me was viewing changes while logged in. I am used to testing site changes in a separate browser as an anonymous user, but per the instructions (https://www.drupal.org/node/2598914):

"IMPORTANT NOTE: Accessing the site as an anonymous user still makes use of caching even when local development settings have been enabled. You must be logged in to view your site with caches disabled."