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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With