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.

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