Disable Twig compilation cache for a specific template

Is there a way to disable Twig compilation cache for a specific template?

I use Twig for my email templates. When someone updates these templates, they are written to disk. Although in production I have to clear the entire cache, otherwise the updates will not be noticed.

This is why I want to disable Twig cache for these specific templates. I don't mind the extra processing power, since clearing my entire cache is a big hit on performance.

+7
source share
1 answer

I think that your answer may not be to disable the cache for a specific template, but to clear the cache for the template after updating it. I have not tested the code below, but it seems reasonable. Play with him a little

In your action / service that saves the template (after saving the template):

$fileCache = $this->container->get('twig')->getCacheFilename('AcmeDemoBundle:Default:index.html.twig'); if (is_file($fileCache)) { @unlink($fileCache); } 

For more information, see how twig cache files are transferred to /vendor/twig/twig/lib/Twig/Environment.php (\ Twig_Environment) - loadTemplate () method.

+2
source

All Articles