Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Twig compilation cache for a particular template

Tags:

php

twig

symfony

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

I'm using Twig for my email templates. When someone updates these templates, they are written to disk. Although in production, I should clear the entire cache otherwise the updates aren't noticed.

That's why I want to disable the Twig cache for these particular templates. I don't mind the extra processing power, as clearing my entire cache is a bigger performance hit.

like image 456
hvtilborg Avatar asked Jan 09 '13 11:01

hvtilborg


1 Answers

I think your answer might be not in disabling cache for a specific template, but clearing the cache for a template after it's been updated. I haven't tested the code below, but it seems reasonable. Play around with it a bit

In your action/service that saves a template (after the template has been saved):

$fileCache = $this->container->get('twig')->getCacheFilename('AcmeDemoBundle:Default:index.html.twig');

if (is_file($fileCache)) {
    @unlink($fileCache);
}

For more information, check out how twig cache files are handed in /vendor/twig/twig/lib/Twig/Environment.php (\Twig_Environment) -- method loadTemplate().

like image 69
Mike Avatar answered Oct 19 '22 15:10

Mike