2

When using cache tag with pagination should the tag go outside the pagination tag?

{% cache %}
<div class="grid-x grid-padding-x" data-equalizer data-equalize-on="medium">
    {% paginate craft.entries.section('employment').endDate('>= ' ~ now).limit(10) as pageInfo, pageEntries %}
    {% for entry in pageEntries %}
        <div class="small-12 medium-6 large-6 cell">
            <div class="news-card" data-equalizer-watch>
                <div class="card-section text-center">
                    <h5><a href="{{ entry.url }}">{{ entry.title }}</a></h5>
                    <p>
                    <span class="label my-purple">
                        {% if entry.openUntilFilled %}
                            Open Until Filled
                        {% else %}
                            Job Closing: {{ entry.endDate.format('M j, Y') }}
                        {% endif %}
                    </span>
                    </p>
                    <p>{{ entry.body|length > 1 ? entry.body|striptags|slice(0, 150) ~ '...' : entry.body }}</p>
                    <p><a class="more" href="{{ entry.url }}">Read More</a></p>
                </div>
            </div>
        </div>
    {% endfor %}
</div>
<div class="grid-x grid-padding-x">
    <div class="small-12 cell">
        {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous Page</a>&nbsp;{% endif %}
        {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next Page</a>{% endif %}
    </div>
</div>
{% endcache %}
1

Yes, the database query you'd want to cache is this:

{% paginate craft.entries.section('employment').endDate('>= ' ~ now).limit(10) as pageInfo, pageEntries %}

So to get the benefits of caching, it needs to be within the cache tag.

|improve this answer|||||
  • Just to be clear and avoid confusion - Craft does not cache the query; the cache tags only cache the output HTML. – Matt Wilcox Aug 8 '17 at 9:14

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.