<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>

CC BY-SA 3.0
2015-12-31T17:48:17.210Z
2015-12-31T17:48:17.210Z
Brad Bell
<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>

<cache>
Cache until expiryDate
# 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 for the body of the two cache tags to keep things DRY.


---

