I have a structure which controls my main site navigation. It's the same across the site so it is best to cache globally as opposed to a cache of the navigation or every url.
The last answer on this pages says that you can use Twig's replace filter to achieve this: Would you use a Structure as navigation over multiple sections (channel entries, indexes and singles)?
My code below shows the active class being placed on the li tag but that doesn't work with the cache globally.
Can it be done with Twig's replace filter?
{% cache globally for 3 years %}
{% set entries = craft.entries.section('navigation') %}
<ul class="right">
{% nav entry in entries %}
<li class="{{ entry.hasDescendants() ? 'has-dropdown' : "" }}{{ craft.request.segment(1) == entry.slug ? ' active' : "" }}">
{# Check for entry type / get related entry #}
{% if entry.type == 'customLink' %}
{# Link to a custom url #}
<a href="{{ entry.customUrl }}">{{ entry.title }}</a>
{% elseif entry.type == 'relatedEntryLink' %}
{% set linkedEntry = entry.relatedEntry.limit(1).first() %}
<a href="{{ linkedEntry.url }}">{{ entry.title }}</a>
{% else %}
{# Link to a structure entry #}
<a href="{{ entry.url }}">{{ entry.title }}</a>
{% endif %}
{# Repeat this for child entries #}
{% ifchildren %}
<ul class="dropdown">{% children %}</ul>
{% endifchildren %}
</li>
{% endnav %}
</ul>
{% endcache %}