Is it possible to use environment variables as the cache duration? I'm trying to set a cache duration as an environment variable. 

I set up various environment variables and know they are being referenced correctly in my templates because I can print out the values of the variables in my templates.

return array(

    '*' =&gt; array(
        'environmentVariables' =&gt; array(
            'cacheDuration'          =&gt; '1 week',
            'cacheDurationQuantity'  =&gt; 1,
            'cacheDurationUnit'      =&gt; 'week',
        ),
    ),
);


I initially started with this cache tag...

{% cache for craft.config.environmentVariables.cacheDuration unless craft.config.devMode %}...{% endcache %}


...but I got this error:


  Unexpected token "name" of value "craft" ("number" expected)


This had me thinking that perhaps Craft or Twig is expecting an integer immediately after the "for" keyword. Thus, I broke my cacheDuration variable into two parts: quantity which is an integer and unit which is a string.

{% cache for craft.config.environmentVariables.cacheDurationQuantity craft.config.environmentVariables.cacheDurationUnit unless craft.config.devMode %}...{% endcache %}


I still got same error as above. This inspired me to try:

{% set duration = 1 %}
{% cache for duration craft.config.environmentVariables.cacheDurationUnit unless craft.config.devMode %}...{% endcache %}


This time, I got a slightly different error.


  Unexpected token "name" of value "duration" ("number" expected)


This has me thinking that any variable evaluated as expected in the duration parameter of the cache tag. However, other variables (like craft.config.devMode) are interpreted fine for other parameters of the cache tag.

Can anyone else confirm this or share insight into this?
The error you're encountering is part of how Pixel &amp; Tonic wrote the cache tag. In short, variables will not work for what you're trying to accomplish. For the longer explanation...

Tags in Twig are made up of tokens. So strings, numbers, variables, functions, tags, and filters are all tokens. When Twig is parsing a tag, it reads all the tokens and checks it against rules in a Twig extension.

The cache tag after seeing a 'for' token will only accept a number as the next token. Not a variable, or a string, or anything else. The bit typed into the template must be a number. For the duration unit, it checks what's there against a list they have already defined. Again, a variable won't work here. Likewise, the 'until' token looks for a date that can be parsed by PHP's strtotime, and 'unless' looks for something that gives it true or false.

You can accomplish the very same thing using built-in config settings. The cacheDuration setting will do as it says using the PHP Interval Specification. If you have a specific cache storage you'd like to use you can use cacheMethod. Setting these will let you use the cache tag without any extra configuration on the tag itself.