4

I have a route set up with a tag: news/tag/[tag] => news/_tag

How do I generate urls that will match that route for all possible tags (including tags with punctuation)?

How do I resolve that url back to a tag (including tags with punctuation)?

Note that twigs url_encode filter href="{{tag.name|url_encode}}" will generate urls that don't route to the template for some tags.

1 Answer 1

4

To generate the url, use the slugify plugin: href="{{tag.name|slugify}}". Slugify uses Craft's slug technology, so the slugs are always recognized by the router.

To resolve the url back to a tag, use search in the news/_tag template:

{% set thisTag = craft.tags.search('name:"'~tag~'"').first() %}

craft.tags.name(tag).first() will often work, but not for all slugs, so this is also possible, and may be more efficient if that is an issue:

{% set thisTag = craft.tags.name(tag).first() %}
{% if not thisTag %} {% set thisTag = craft.tags.search('name:"'~tag~'"').first() %} {% endif %}

Your Answer

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

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