11

Can I disable template caching for local development? For example: setting a environmentVariable in a config file? Or is this possible in the future?

  • Yes. See my answer to this other question about caching. – carlcs Jul 2 '14 at 7:14
17

When working with multiple environments you most likely want to disable caching for all but one environment, because Craft saves the absolute URL to entries/assets in the cache.

I achieved this by setting a custom variable in general.php

'craft.dev' => array(
    'cache' => true,
    'environmentVariables' => array(...),
)

 '192.168.178.20' => array(
    'cache' => false,
    'environmentVariables' => array(...),
)

and then add a conditional to the cache function in the template

{% cache if craft.config.cache %}
    {# ... #}
{% endcache %}

With this setup I can now access the cached content on craft.dev and open the site on another devices in the network on 192.168.178.20 without having problems with cached urls.

I first posted this on another question about caching: "What are the best practices for using the {% cache %} tag?", which might also be of interest for you!

(Edit: Since Craft 2.2 it is possible to use if condition with the cache tag, so you don't have to do messy constructs like unless not any more.)

32

As of Craft 2.4, it is now possible to use the enableTemplateCaching config value.

'enableTemplateCaching' => false,

Simply add that to your local environment in config/general.php, and it'll disable the cache tags entirely.

4

I found this post and the answer by @carlcs helpful. However, I find the phrasing unless not in the cache logic a bit hard to wrap my head around. I want to note here an alternative to the syntax above that I found more intuitive:

general.php

return array(
    'live' => array(
        'cache' => true
    ),

    'local' => array(
        'cache' => false
    )
)

cache tag which only runs in live environment

{% cache if craft.config.cache %}
   ...
{% endcache %}
  • Using if with the cache tag wasn't possible when I wrote the answer. But you're right, it reads better and I actually use it myself that way. Thanks for the reminder to change it here, too. – carlcs Mar 13 '15 at 18:18
1

I'd prefer to use caching all the time. Since developing with caching disabled can be really confusing when something refuses to work in production with caching enabled. If you develop with caching enabled, you'd probably find issues while developing.

Better to clear the cache folder if you need to refresh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.