0

I'm trying to get pagination and cache to work together but it's failing.

Here's my current code:

{% cache globally using key
   craft.request.path ~ '/p' ~ craft.request.getPageNum
   for 2 hours %}

{% paginate topicEntries.limit(10)as pageInfo, topicEntries %}

  {% for topic in topicEntries %}

      more code here

  {% endfor %}

{% endcache %

When I use this I get a template error that Variable pageInfo does not exist.

Then if I put the opening cache tag below the paginate tag. The first page shows 10 entries, but each page after that only displays 1 entry per page.

{% paginate topicEntries.limit(10)as pageInfo, topicEntries %}
 {% cache globally using key
    craft.request.path ~ '/p' ~ craft.request.getPageNum
    for 2 hours %}

  {% for topic in topicEntries %}

      more code here

  {% endfor %}

{% endcache %}

What I need to know is how to cache paginated entries so it's 10 per page and when a new entry is added the cache is cleared correctly.

Please advise.

2
  • First of all, is your code actually {% paginate topicEntries.limit(10)as pageInfo, topicEntries %}? If so, you're missing a space between limit(10) and as. Guessing that's just an error here on SE, since that'd give you some other error. Also, I suspect that there are some code that accesses pageInfo that's outside of the code that's shown here? If pageInfo is created inside the cache tag, you can't access it outside. Seeing the rest of your template code would be useful. Apr 10, 2018 at 13:31
  • @AndréElvan this template is an include - have posted the full include file here: pastebin.com/A0b8Pfv1
    – CreateSean
    Apr 10, 2018 at 17:38

1 Answer 1

3
+50

Based on the code you posted on pastebin, it seems like the problem is that you have your pagination outside of the cache tags (assuming this was the case when you had caching implemented). And, I'm assuming that inside your {% include "_includes/pagination" %} you're trying to access pageInfo, which is created by the pagination tag.

To make this work, and be able to implement efficient caching, you need to include your pagination inside your cache tags. In fact, I would also include the logic that creates the topicEntries element criteria above where you have the pagination tag today, inside the cache tag, as it creates DB queries and is directly related to the content you want to cache.

The overall structure of your code should then be:

{% cache globally using key craft.request.path ~ '/p' ~ craft.request.getPageNum for 2 hours %}
    <ul class="feat-box-container">
        {#  lots of code to get topicEntries #}

        {% paginate topicEntries.limit(10) as pageInfo, topicEntries %}

        {% for topic in topicEntries %}
            {# code that outputs list items #}
        {% endfor %}
    </ul>

    {% include "_includes/pagination" %}
{% endcache %}
1
  • 1
    This appears to be working, however when I add a new entry to the section, the cache is not broken and new entries do not display.
    – CreateSean
    Apr 11, 2018 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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