Answers.Body: <p>Wes, there is a built in config setting for <code>cacheDuration</code>. See docs for <a href="http://buildwithcraft.com/docs/config-settings#cacheDuration" rel="nofollow">General config settings</a>:</p>
  
  <blockquote>
    <p>"cacheDuration": Also used by the <code>{% cache %}</code> template tag if no expiration time is specified as a parameter.</p>
  </blockquote>
  
  <p>Example config:</p>
  
  <pre><code>return array(
      '*' =&gt; array(
          'cacheDuration' =&gt; '4 years',
          'environmentVariables' =&gt; array(),
      ),
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T13:14:45.340Z
Answers.Text: If Bryan is right, there is probably no difference in these cache settings. Undeleted this again.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T13:16:17.690Z
Answers.Text: I double checked the template caching in the source, and made sure it used the config settings. So yeah, we're both correct.
Answers.UserDisplayName: Bryan Redeagle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-18T20:18:00.543Z
Answers.Text: I originally accepted this, but I want to make sure that this is correct. Is there a difference between data caching and file caching duration? The docs say this parameter is the default for data caching.
Answers.UserDisplayName: Wes Rice
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-18T20:25:07.070Z
Answers.Text: There is also the possibility that I'm misunderstanding caching in general here and that all caching is "data" caching. However, my understanding is that template caching compiles the twig templates and caches them, which in my mind is file based caching, which is different than data caching.
Answers.UserDisplayName: Wes Rice
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-18T21:11:16.443Z
Answers.Text: Ouuups, I did't see that this is for data caching. My answer is definitely wrong then. Can I delete the answer or what can I do now?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-18T19:08:23.940Z
Answers.LastActivityDate: 2015-10-30T18:20:25.203Z
Answers.LastEditDate: 2015-10-30T18:20:25.203Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>You could use the cache tags <a href="https://craftcms.com/docs/templating/cache#until" rel="nofollow"><code>until</code></a> parameter and calculate the next expiration date from your "cacheDuration" config variable.</p>
  
  <pre><code>return array(
      '*' =&gt; array(
          'cacheDuration' =&gt; 604800, // 1 week
      ),
  );
  </code></pre>
  
  <p>Use Twig's <a href="http://twig.sensiolabs.org/doc/filters/round.html" rel="nofollow"><code>round</code></a> filter to round up from "now" to the next week.</p>
  
  <pre><code>{% set duration = craft.config.cacheDuration %}
  {% set next = (now|date('U') / duration)|round(0, 'ceil') * duration %}
  
  {% cache until next|date('c') %}
  {% endcache %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T13:12:48.820Z
Answers.LastActivityDate: 2016-02-06T00:02:43.153Z
Answers.LastEditDate: 2016-02-06T00:02:43.153Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>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...</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>You can accomplish the very same thing using built-in config settings. The <a href="http://buildwithcraft.com/docs/config-settings#cacheDuration">cacheDuration</a> setting will do as it says using the <a href="http://www.php.net/manual/en/dateinterval.construct.php">PHP Interval Specification</a>. If you have a specific cache storage you'd like to use you can use <a href="http://buildwithcraft.com/docs/config-settings#cacheMethod">cacheMethod</a>. Setting these will let you use the cache tag without any extra configuration on the tag itself.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T00:22:57.503Z
Answers.LastActivityDate: 2014-06-19T00:22:57.503Z
Answers.UserDisplayName: Bryan Redeagle
Body: <p>I'm trying to set a cache duration as an environment variable. </p>
  
  <p>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.</p>
  
  <pre><code>return array(
  
      '*' =&gt; array(
          'environmentVariables' =&gt; array(
              'cacheDuration'          =&gt; '1 week',
              'cacheDurationQuantity'  =&gt; 1,
              'cacheDurationUnit'      =&gt; 'week',
          ),
      ),
  );
  </code></pre>
  
  <p>I initially started with this cache tag...</p>
  
  <pre><code>{% cache for craft.config.environmentVariables.cacheDuration unless craft.config.devMode %}...{% endcache %}
  </code></pre>
  
  <p>...but I got this error:</p>
  
  <blockquote>
    <p>Unexpected token "name" of value "craft" ("number" expected)</p>
  </blockquote>
  
  <p>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.</p>
  
  <pre><code>{% cache for craft.config.environmentVariables.cacheDurationQuantity craft.config.environmentVariables.cacheDurationUnit unless craft.config.devMode %}...{% endcache %}
  </code></pre>
  
  <p>I still got same error as above. This inspired me to try:</p>
  
  <pre><code>{% set duration = 1 %}
  {% cache for duration craft.config.environmentVariables.cacheDurationUnit unless craft.config.devMode %}...{% endcache %}
  </code></pre>
  
  <p>This time, I got a slightly different error.</p>
  
  <blockquote>
    <p>Unexpected token "name" of value "duration" ("number" expected)</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Can anyone else confirm this or share insight into this?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-18T18:32:47.363Z
Comments.Text: Can you please tell me why you want to use cache duration? I am curious because I did not see a use case for it yet.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-18T20:21:22.313Z
Comments.Text: I have cache tags in several places throughout my site. The cache duration might need to change from time to time depending on how often the site will be updated, which will vary during different parts of the year.
  
  The idea is to abstract the duration into one spot to where I can change the duration for all tags easily.
Comments.UserDisplayName: Wes Rice
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-18T21:13:47.490Z
Comments.Text: Aren't cached template parts updated as soon as you make changes to your entries in the backend? That's why I don't understand the need to set a duration.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-18T21:24:12.073Z
Comments.Text: True, but I actually have some parts of templates that are being pulled from other data sources, like twitter. Setting cache durations via a variable for those situations would be convenient.
Comments.UserDisplayName: Wes Rice
Tags: <cache><variable>
Title: Is it possible to use environment variables as the cache duration?
ThreadText: # Is it possible to use environment variables as the cache duration?
  
  (Asked by: username_0 on 2014-06-18)
  
  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(
  
      '*' => array(
          'environmentVariables' => array(
              'cacheDuration'          => '1 week',
              'cacheDurationQuantity'  => 1,
              'cacheDurationUnit'      => '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?
  
  **Tags:** cache, variable
  
  > **Comments:**
  > **username_1**: Can you please tell me why you want to use cache duration? I am curious because I did not see a use case for it yet.  
  > **username_0**: I have cache tags in several places throughout my site. The cache duration might need to change from time to time depending on how often the site will be updated, which will vary during different parts of the year.  The idea is to abstract the duration into one spot to where I can change the duration for all tags easily.  
  > **username_1**: Aren't cached template parts updated as soon as you make changes to your entries in the backend? That's why I don't understand the need to set a duration.  
  > **username_0**: True, but I actually have some parts of templates that are being pulled from other data sources, like twitter. Setting cache durations via a variable for those situations would be convenient.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2014-06-19. Score: 6 | Accepted answer
  
  The error you're encountering is part of how Pixel & 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](http://buildwithcraft.com/docs/config-settings#cacheDuration) setting will do as it says using the [PHP Interval Specification](http://www.php.net/manual/en/dateinterval.construct.php). If you have a specific cache storage you'd like to use you can use [cacheMethod](http://buildwithcraft.com/docs/config-settings#cacheMethod). Setting these will let you use the cache tag without any extra configuration on the tag itself.
  
  
  ---
  
  ### Answer by username_1 on 2014-06-18. Score: 4
  
  Wes, there is a built in config setting for `cacheDuration`. See docs for [General config settings](http://buildwithcraft.com/docs/config-settings#cacheDuration):
  
  > "cacheDuration": Also used by the `{% cache %}` template tag if no expiration time is specified as a parameter.
  
  Example config:
  
  ```
  return array(
      '*' => array(
          'cacheDuration' => '4 years',
          'environmentVariables' => array(),
      ),
  );
  
  ```
  
  > **Comments:**
  > **username_0**: I originally accepted this, but I want to make sure that this is correct. Is there a difference between data caching and file caching duration? The docs say this parameter is the default for data caching.  
  > **username_0**: There is also the possibility that I'm misunderstanding caching in general here and that all caching is "data" caching. However, my understanding is that template caching compiles the twig templates and caches them, which in my mind is file based caching, which is different than data caching.  
  > **username_1**: Ouuups, I did't see that this is for data caching. My answer is definitely wrong then. Can I delete the answer or what can I do now?  
  > **username_1**: If username_2 is right, there is probably no difference in these cache settings. Undeleted this again.  
  > **username_2**: I double checked the template caching in the source, and made sure it used the config settings. So yeah, we're both correct.  
  
  ---
  
  ### Answer by username_1 on 2014-06-19. Score: 2
  
  You could use the cache tags [`until`](https://craftcms.com/docs/templating/cache#until) parameter and calculate the next expiration date from your "cacheDuration" config variable.
  
  ```
  return array(
      '*' => array(
          'cacheDuration' => 604800, // 1 week
      ),
  );
  
  ```
  
  Use Twig's [`round`](http://twig.sensiolabs.org/doc/filters/round.html) filter to round up from "now" to the next week.
  
  ```
  {% set duration = craft.config.cacheDuration %}
  {% set next = (now|date('U') / duration)|round(0, 'ceil') * duration %}
  
  {% cache until next|date('c') %}
  {% endcache %}
  
  ```
  
  
  ---
  
  