Answers.Body: <blockquote>
    <p>Caching is hard.</p>
  </blockquote>
  
  <p><a href="https://twitter.com/codinghorror/status/506010907021828096" rel="nofollow">https://twitter.com/codinghorror/status/506010907021828096</a></p>
  
  <p>Something like this should work.</p>
  
  <pre><code>{% if newsExpiration is defined %}
      {% cache until newsExpiration.expiryDate  %}
          ...
      {% endcache %}
  {% else %}
      {% cache %}
          ...
      {% endcache %}
  {% endif %}
  </code></pre>
  
  <p>You'd probably want to use an <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow">include</a> for the body of the two cache tags to keep things <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-31T17:48:17.210Z
Answers.LastActivityDate: 2015-12-31T17:48:17.210Z
Answers.UserDisplayName: Brad Bell
Body: <p>Caching is hard.</p>
  
  <p>I want to delete the cache when a news entry hits it's expiryDate. So I wrote</p>
  
  <pre><code>{% set newsExpiration = craft.entries.section('news').first()%}
  
  {% cache until newsExpiration.expiryDate  %}
     Bla
  {% endcache %}
  </code></pre>
  
  <p>Now the news entry has expired and no news is present so there is an error: </p>
  
  <blockquote>
    <p>Impossible to access an attribute ("expiryDate") on a null variable</p>
  </blockquote>
  
  <p>I then wanted to write a conditional to see if <code>newsExpiration</code> is defined.</p>
  
  <pre><code>{% if newsExpiration is defined %}
     {% cache until newsExpiration.expiryDate  %}
  {% else %}
     {% cache %}
  {% endif %}
  </code></pre>
  
  <p>But that also throws an error because Twig is expecting a closing tag to the cache.</p>
  
  <blockquote>
    <p>Unexpected "else" tag (expecting closing tag for the "cache" tag
    defined near line xy).</p>
  </blockquote>
  
  <p>I there a proper way to define the cache tag including both present and absent entries without query conditionals?</p>
  
Tags: <cache>
Title: Cache until expiryDate
ThreadText: # Cache until expiryDate
  
  (Asked by: username_0 on 2015-12-31)
  
  Caching is hard.
  
  I want to delete the cache when a news entry hits it's expiryDate. So I wrote
  
  ```
  {% set newsExpiration = craft.entries.section('news').first()%}
  
  {% cache until newsExpiration.expiryDate  %}
     Bla
  {% endcache %}
  
  ```
  
  Now the news entry has expired and no news is present so there is an error:
  
  > Impossible to access an attribute ("expiryDate") on a null variable
  
  I then wanted to write a conditional to see if `newsExpiration` is defined.
  
  ```
  {% if newsExpiration is defined %}
     {% cache until newsExpiration.expiryDate  %}
  {% else %}
     {% cache %}
  {% endif %}
  
  ```
  
  But that also throws an error because Twig is expecting a closing tag to the cache.
  
  > Unexpected "else" tag (expecting closing tag for the "cache" tag
  > defined near line xy).
  
  I there a proper way to define the cache tag including both present and absent entries without query conditionals?
  
  **Tags:** cache
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-31. Score: 2 | Accepted answer
  
  > Caching is hard.
  
  <https://twitter.com/codinghorror/status/506010907021828096>
  
  Something like this should work.
  
  ```
  {% if newsExpiration is defined %}
      {% cache until newsExpiration.expiryDate  %}
          ...
      {% endcache %}
  {% else %}
      {% cache %}
          ...
      {% endcache %}
  {% endif %}
  
  ```
  
  You'd probably want to use an [include](http://twig.sensiolabs.org/doc/tags/include.html) for the body of the two cache tags to keep things [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself).
  
  
  ---
  
  