<p>So i found an answer, was a little more manual but it works. </p>

<p>I added a call back to the onMoveElement to automatically more manually clear the cache.  This was added to my plugins init() function</p>

<pre><code>craft()-&gt;structures-&gt;onMoveElement = function(Event $event) {
      $element = $event-&gt;params['element'];
      craft()-&gt;templateCache-&gt;deleteCachesByElementId($element['id']);
};
</code></pre>

<p>Was interesting to me that i only had to do this for reordering sturctures while adding/deleting elements automatically cleared the cache with no event code.</p>

<p>Not sure if this was a bug or by design?</p>

CC BY-SA 3.0
2014-12-26T21:00:25.033Z
2014-12-26T21:00:25.033Z
Keith Mancuso
<p>You could also use the cache tag in combination with a dynamic key, like the specific ordering as a key. It will however create stale cache entries, so be sure to use this only if it won't be a problem, i.e. rarely updates and/or cache cleared regularly. For custom elements reordering the given on-move approach seems better, but this method could perhaps be of use for the standard entries.</p>

CC BY-SA 3.0
2016-01-14T23:30:29.227Z
2016-01-14T23:30:29.227Z
Wytse van der Velde
<p>It sounds like you're trying to use the cache tag for your plugin's template files from inside the control panel.</p>

<p>If that's the case, don't. :)</p>

<p>The <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow">{% cache %}</a> tag was designed purely to be used for front-end site templates.  You'll run into all sorts of weirdness using it from templates in the control panel.</p>

CC BY-SA 3.0
2014-12-21T01:06:14.343Z
No no this is for front end templates.  I'm outputting the menu onto the site
Keith Mancuso
CC BY-SA 3.0
2014-12-21T01:08:05.407Z
Ahh, can you update the questions with some relevant example template code?
Brad Bell
CC BY-SA 3.0
2014-12-21T01:08:56.547Z
Sure, should have included that to begin with
Keith Mancuso
CC BY-SA 3.0
2014-12-21T01:37:51.653Z
hmm so it might be clearing itself after all...hmm
Keith Mancuso
CC BY-SA 3.0
2014-12-21T01:39:10.563Z
so adding new nodes seems to clear it actually but reordering doesnt?
Keith Mancuso
CC BY-SA 3.0
2014-12-21T02:59:32.540Z
Is the underlying data here stored in a structured section?
Brad Bell
CC BY-SA 3.0
2014-12-21T06:38:50.473Z
It's a custom element with relationships to structures
Keith Mancuso
CC BY-SA 3.0
2014-12-26T20:41:05.917Z
Hey Brad, i wondered if you had any more thoughts on this one.  No matter what i do i cant get template caches to clear when i reorder?  I checked all the code for entries and and i dont see anything different with my element since  everythings just calling actionMoveElement() on the Stuctures Controller?
Keith Mancuso
CC BY-SA 3.0
2014-12-21T01:05:17.437Z
2014-12-21T01:05:17.437Z
Brad Bell
<p>I've developed a custom menus plugin which has been working great</p>

<p><a href="https://github.com/familiar-studio/craft-menus" rel="nofollow">https://github.com/familiar-studio/craft-menus</a></p>

<p>Im trying to get it work with the build in template caching and realizing the reorder actions are not triggering the caches to clear properly?</p>

<p>The template is using the "getNodes(menuHandle)" variable
<a href="https://github.com/familiar-studio/craft-menus/blob/master/menus/variables/MenusVariable.php" rel="nofollow">https://github.com/familiar-studio/craft-menus/blob/master/menus/variables/MenusVariable.php</a></p>

<p>which just uses standard element criteria? We are also using native structures so is there some way to specifically tell it to clear on reordering as well?</p>

<p><strong>UPDATE:</strong> here is the code thats in my _layout that im hoping will clear when i reorder or add new elements</p>

<pre><code>{% cache %}
  &lt;ul class="nav navbar-nav navbar-right"&gt;
      {% nav node in craft.menus.getNodes('utilityMenu') %}
         &lt;li class="{{ node.children|length ? 'dropdown' }}
            {{ node.active ? 'active' }}"&gt;

            &lt;a href="{{node.link}}"
                {% if node.children|length %}
                   class="dropdown-toggle" data-toggle="dropdown"
                {% endif %}&gt;

                {{ node.title }}
            &lt;/a&gt;

            {% ifchildren %}

                &lt;ul class="dropdown-menu" role="menu"&gt;
                    &lt;div class="arrow"&gt;&lt;/div&gt;
                    {% children %}
                &lt;/ul&gt;

            {% endifchildren %}
        &lt;/li&gt;
     {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>{% endcache %}</p>

<p><strong>UPDATE 2:</strong> So on deeper investivation it appears that the issue is only that it doesnt clear the cache when the structure is reordered, editing and saving nodes seems to clear it after all.</p>

<cache><elementtype>
Automatic cache clearing when reordering custom element type
# Automatic cache clearing when reordering custom element type

(Asked by: username_0 on 2014-12-20)

I've developed a custom menus plugin which has been working great
https://github.com/familiar-studio/craft-menus
Im trying to get it work with the build in template caching and realizing the reorder actions are not triggering the caches to clear properly?
The template is using the "getNodes(menuHandle)" variable
https://github.com/familiar-studio/craft-menus/blob/master/menus/variables/MenusVariable.php
which just uses standard element criteria? We are also using native structures so is there some way to specifically tell it to clear on reordering as well?
UPDATE: here is the code thats in my _layout that im hoping will clear when i reorder or add new elements
{% cache %}
  
      {% nav node in craft.menus.getNodes('utilityMenu') %}
         

            

                {{ node.title }}
            

            {% ifchildren %}

                
                    
                    {% children %}
                

            {% endifchildren %}
        
     {% endnav %}


{% endcache %}
UPDATE 2: So on deeper investivation it appears that the issue is only that it doesnt clear the cache when the structure is reordered, editing and saving nodes seems to clear it after all.

**Tags:** cache, elementtype


---

## 3 answers

### Answer by username_0 on 2014-12-26. Score: 2 | Accepted answer

So i found an answer, was a little more manual but it works. 
I added a call back to the onMoveElement to automatically more manually clear the cache.  This was added to my plugins init() function
craft()->structures->onMoveElement = function(Event $event) {
      $element = $event->params['element'];
      craft()->templateCache->deleteCachesByElementId($element['id']);
};

Was interesting to me that i only had to do this for reordering sturctures while adding/deleting elements automatically cleared the cache with no event code.
Not sure if this was a bug or by design?


---

### Answer by username_1 on 2016-01-14. Score: 0

You could also use the cache tag in combination with a dynamic key, like the specific ordering as a key. It will however create stale cache entries, so be sure to use this only if it won't be a problem, i.e. rarely updates and/or cache cleared regularly. For custom elements reordering the given on-move approach seems better, but this method could perhaps be of use for the standard entries.


---

### Answer by username_2 on 2014-12-21. Score: 0

It sounds like you're trying to use the cache tag for your plugin's template files from inside the control panel.

If that's the case, don't. :)

The [{% cache %}](http://buildwithcraft.com/docs/templating/cache) tag was designed purely to be used for front-end site templates. You'll run into all sorts of weirdness using it from templates in the control panel.

> **Comments:**
> **username_0**: No no this is for front end templates.  I'm outputting the menu onto the site  
> **username_2**: Ahh, can you update the questions with some relevant example template code?  
> **username_0**: Sure, should have included that to begin with  
> **username_0**: hmm so it might be clearing itself after all...hmm  
> **username_0**: so adding new nodes seems to clear it actually but reordering doesnt?  
> **username_2**: Is the underlying data here stored in a structured section?  
> **username_0**: It's a custom element with relationships to structures  
> **username_0**: Hey Brad, i wondered if you had any more thoughts on this one.  No matter what i do i cant get template caches to clear when i reorder?  I checked all the code for entries and and i dont see anything different with my element since  everythings just calling actionMoveElement() on the Stuctures Controller?  

---

