0

I have a long entry list of authors and I am using global cache to help with the page load, but this global cache prevents the if statement from adding selected to the appropriate option. When I remove the global cache everything performs as intended.

<select name="filter-by-author" onchange="window.location.href=this.value;">
    <option value="/blog">All Authors</option>
        {% cache globally %}
            {% nav author in craft.users({ group: 'blogAuthors', order: 'firstName', limit: null }) %}
                <option value="{{ url('blog/author/'~author.username) }}"
                {% if craft.request.getLastSegment() == author.username %}selected{% endif %}>
                {{ author.name | title }}
                </option>
            {% endnav %}
        {% endcache %}
</select>
2

This is exactly what caching is supposed to do... flatten the Twig code down to basic HTML, and serve that up instead of dynamically rendering it. Using globally means that the HTML would be identical on every page, which is not really what you want here.

Get rid of globally, so it can cache separately for each page. Since the URL is supposed to affect how that snippet is rendered, you don't want to cache globally.

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