2

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 %}
  <ul class="nav navbar-nav navbar-right">
      {% nav node in craft.menus.getNodes('utilityMenu') %}
         <li class="{{ node.children|length ? 'dropdown' }}
            {{ node.active ? 'active' }}">

            <a href="{{node.link}}"
                {% if node.children|length %}
                   class="dropdown-toggle" data-toggle="dropdown"
                {% endif %}>

                {{ node.title }}
            </a>

            {% ifchildren %}

                <ul class="dropdown-menu" role="menu">
                    <div class="arrow"></div>
                    {% children %}
                </ul>

            {% endifchildren %}
        </li>
     {% endnav %}
</ul>

{% 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.

2

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?

| improve this answer | |
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 %} 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.

| improve this answer | |
  • No no this is for front end templates. I'm outputting the menu onto the site – Keith Mancuso Dec 21 '14 at 1:06
  • Ahh, can you update the questions with some relevant example template code? – Brad Bell Dec 21 '14 at 1:08
  • Sure, should have included that to begin with – Keith Mancuso Dec 21 '14 at 1:08
  • hmm so it might be clearing itself after all...hmm – Keith Mancuso Dec 21 '14 at 1:37
  • so adding new nodes seems to clear it actually but reordering doesnt? – Keith Mancuso Dec 21 '14 at 1:39
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.