<p>To generate the url, use the <a href="https://gist.github.com/boboldehampsink/7354431/download#" rel="nofollow">slugify</a> plugin: <code>href="{{tag.name|slugify}}"</code>. Slugify uses Craft's slug technology, so the slugs are always recognized by the router.</p>

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

<pre class="lang-twig prettyprint-override"><code>{% set thisTag = craft.tags.search('name:"'~tag~'"').first() %}
</code></pre>

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

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

CC BY-SA 3.0
2014-07-11T17:45:04.297Z
2014-07-11T17:45:04.297Z
Marion Newlevant
<p>I have a route set up with a tag:
news/tag/[tag] => news/_tag</p>

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

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

<p>Note that twigs <a href="http://twig.sensiolabs.org/doc/filters/url_encode.html" rel="nofollow">url_encode</a> filter <code>href="{{tag.name|url_encode}}"</code> will generate urls that don't route to the template for some tags.</p>

<templating><tags><routes>
Bullet-proofing tags with punctuation
# Bullet-proofing tags with punctuation

(Asked by: username_0 on 2014-07-11)

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](http://twig.sensiolabs.org/doc/filters/url_encode.html) filter `href="{{tag.name|url_encode}}"` will generate urls that don't route to the template for some tags.

**Tags:** templating, tags, routes


---

## 1 answers

### Answer by username_0 on 2014-07-11. Score: 4 | Accepted answer

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


---

