<p>Hard to know for sure without seeing some plugin code, but my guess is you'll want to use the <a href="https://craftcms.com/classreference/services/TemplateCacheService" rel="nofollow">TemplateCacheService's</a> <a href="https://craftcms.com/classreference/services/TemplateCacheService#includeElementInTemplateCaches-detail" rel="nofollow">includeElementInTemplateCaches</a> similar to how <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/BaseTemplate.php" rel="nofollow">BaseTemplate's</a> <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/BaseTemplate.php#L77" rel="nofollow">_includeElementInTemplateCaches</a> does.</p>

CC BY-SA 3.0
2016-08-18T07:06:29.487Z
Thank you. I updated my question with some more details and plugin code. I don't understand yet how I would relate the event elements to that certain cached template part. Do I need to use a special key `{% cache key = "xy %}` and refer to that in the plugin? Would it also be a easier solution to maybe read the event elements in twig normally with `craft.entries()` and then pass it to the plugin?
exophunk
CC BY-SA 3.0
2016-08-18T07:36:18.083Z
Update: I used the `includeElementInTemplateCaches` in my plugin as you proposed and it works perfectly. I had to understand the idea, that it is doing this while being "inside" a cache tag and therefor I don't have to relate it to a certain templatecache :) Thank you!
exophunk
CC BY-SA 3.0
2016-08-17T17:22:16.983Z
2016-08-17T17:22:16.983Z
Brad Bell
<p>I wrote a little plugin that reads some entries (Events) from my events section, orders them in a calendar-like structure and returns them to be displayed in a template. (I couldn't do this twig-only)</p>

<p>Now, I would like to use <code>{% cache %}</code> on the calendar. I know that Craft normally busts the template cache when updating/adding an element if it detects, that those elements were used in a cached template.</p>

<p>Now as I am fetching the elements in my custom plugin with </p>

<p><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);</code></p>

<p>I assume, that Craft can't know when to bust the cached output of the plugin.</p>

<p>Is there a way (with the TemplateCacheService for example) to make sure that this cache also gets busted when saving/updating one of my events?</p>

<p>I wonder if I can do this just in twig or if I would have to put a hook before the element gets saved? How does it relate to the cache key?</p>

<p>Any hints, which direction to take?</p>

<p>Thank you,</p>

<p>Robert</p>

<hr>

<h1>Update:</h1>

<p>Okay, I'll provide some more details:</p>

<p>My Plugin looks (simplified) like this:</p>

<pre><code>// MyPluginVariable.php: 
public function getMonthWithEvents($month, $year)
{
    ...
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = 'events';
    $criteria-&gt;date = ['and', '&gt;='.$monthStart, '&lt;='.$monthEnd];
    $eventsInMonth = $criteria-&gt;find();

    $month = [];

    // Put events in month array with dates etc....

    return $month;
}
</code></pre>

<p>In the twig template, I do this:</p>

<pre><code>// agenda.twig
...

{% cache %}

    {% set month = craft.migrosAgenda.getMonthWithEvents(10, 2016) %}

    {% for day in month %}
        ...
        &lt;div class="day"&gt;
            {{ day.date|date('d.M.Y') }}
            {{ day.event.title }}
            {{ day.event.date }}
        &lt;/div&gt;
    {% endfor %}

{% endcache %}
</code></pre>

<plugin-development><cache><elementcriteriamodel>
Define elements that bust cache on save
# Define elements that bust cache on save

(Asked by: username_0 on 2016-08-17)

I wrote a little plugin that reads some entries (Events) from my events section, orders them in a calendar-like structure and returns them to be displayed in a template. (I couldn't do this twig-only)
Now, I would like to use {% cache %} on the calendar. I know that Craft normally busts the template cache when updating/adding an element if it detects, that those elements were used in a cached template.
Now as I am fetching the elements in my custom plugin with 
$criteria = craft()->elements->getCriteria(ElementType::Entry);
I assume, that Craft can't know when to bust the cached output of the plugin.
Is there a way (with the TemplateCacheService for example) to make sure that this cache also gets busted when saving/updating one of my events?
I wonder if I can do this just in twig or if I would have to put a hook before the element gets saved? How does it relate to the cache key?
Any hints, which direction to take?
Thank you,
Robert

Update:
Okay, I'll provide some more details:
My Plugin looks (simplified) like this:
// MyPluginVariable.php: 
public function getMonthWithEvents($month, $year)
{
    ...
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'events';
    $criteria->date = ['and', '>='.$monthStart, '<='.$monthEnd];
    $eventsInMonth = $criteria->find();

    $month = [];

    // Put events in month array with dates etc....

    return $month;
}

In the twig template, I do this:
// agenda.twig
...

{% cache %}

    {% set month = craft.migrosAgenda.getMonthWithEvents(10, 2016) %}

    {% for day in month %}
        ...
        
            {{ day.date|date('d.M.Y') }}
            {{ day.event.title }}
            {{ day.event.date }}
        
    {% endfor %}

{% endcache %}

**Tags:** plugin-development, cache, elementcriteriamodel

---

## 1 answers

### Answer by username_1 on 2016-08-17. Score: 3 | Accepted answer

Hard to know for sure without seeing some plugin code, but my guess is you'll want to use the [TemplateCacheService's](https://craftcms.com/classreference/services/TemplateCacheService) [includeElementInTemplateCaches](https://craftcms.com/classreference/services/TemplateCacheService#includeElementInTemplateCaches-detail) similar to how [BaseTemplate's](https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/BaseTemplate.php) [\_includeElementInTemplateCaches](https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/templating/BaseTemplate.php#L77) does.

> **Comments:**
> **username_0**: Thank you. I updated my question with some more details and plugin code. I don't understand yet how I would relate the event elements to that certain cached template part. Do I need to use a special key `{% cache key = "xy %}` and refer to that in the plugin? Would it also be a easier solution to maybe read the event elements in twig normally with `craft.entries()` and then pass it to the plugin?  
> **username_0**: Update: I used the `includeElementInTemplateCaches` in my plugin as you proposed and it works perfectly. I had to understand the idea, that it is doing this while being "inside" a cache tag and therefor I don't have to relate it to a certain templatecache :) Thank you!  

---

