<p>Ok, so I'm tenatively answering my own question — although I'd like someone to shed light on the <a href="https://craftcms.stackexchange.com/questions/16318/best-solution-for-articles-tag-page">duplicate tag problem</a> — it seems like a combination of #1 and #2 works quite nicely, although I haven't extensively tested it yet.</p>

<pre><code>{% if tag is defined %}
  {% set tag = craft.tags.slug(tag).first() %}
  {% set entries = craft.entries.section('articles').relatedTo(tag) %}
{% else %}
  {% set entries = craft.entries.section('articles') %}
{% endif %}

{% for entry in entries %}
  ...
{% endfor %}
</code></pre>

<p>This is working with the route <code>articles/tag/[tag token]</code> set up. The only issue is when a tag that has a duplicate slug, whether that be within the same tag group or a different one, it returns no results (I'm not great at reading logs, but it doesn't seem to throw any errors either).</p>

<p>Any additions to this answer would be appreciated!</p>

CC BY-SA 3.0
2016-08-11T13:33:36.243Z
I believe this would solve your "duplicate tag issue": Change `{% set tag = craft.tags.slug(tag).first() %}` to `{% set tags = craft.tags.slug(tag) %}`, and then do `{% set entries = craft.entries.section('articles').relatedTo(tags.ids()) %}`. With your current code, the `.first()` tag returned might not actually have any related entries, in cases where there are more than one tag with the same name.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-17T18:23:40.277Z
@MatsMikkelRummelhoff Thanks for the tip, I think for now I'm just going to enforce _no duplicates_ on my client. Yours sounds like a sensible solution, but the idea is to have two distinct tag groups — that method would display all articles, with no easy way of separating them into their groups. Right? I imagine [two routes](https://craftcms.com/docs/routing) would be the only way to achieve this seperation, which is overkill for article tags.
Rob
CC BY-SA 3.0
2016-08-17T18:30:42.720Z
You can better see what I mean [here](http://counsellinganywhere.com/articles) and [here](http://counsellinganywhere.com/articles/tag/a-z-of-therapy-types).
Rob
CC BY-SA 3.0
2016-08-17T19:11:57.930Z
No disrespect, but the "no duplicates" thing doesn't really sound enforceable. Sooner or later, someone's probably going to add a duplicate tag and you're going to have a problem. IMHO, considering the two tag groups are distinct, it wouldn't be overkill at all to create two routes, it'd make sense. Assuming "Types of issues" and "Types of therapy" are your two tag groups, you could have routes like `articles/therapy/{tag}` and `articles/issues/{tag}` – much more semantic than just `articles/tag/{tag}`, and more future proof (perhaps in the future you'll have 3 or 4 tag groups).
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-17T19:16:55.173Z
Since the two routes would obviously point to the same template, it'd be a minimal amount of work to set up, too – all you really need is to add a `.group(tagGroupHandle)` to your `craft.tags` query, where you pull the `tagGroupHandle` from the request URI (either by using a route token, or by looking at the relevant segment via [`craft.request`](https://craftcms.com/classreference/variables/HttpRequestVariable)).
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-11T13:05:15.317Z
2016-08-11T13:05:15.317Z
2017-04-13T12:51:00.160Z
Rob
<p>I've been looking through the forum, it seems there's a few methods to retrieve a tags page but I'm looking for the most efficient method (both speed- and template-wise).</p>

<ol>
<li>Using <a href="https://craftcms.stackexchange.com/questions/828/looking-to-use-a-tag-slug-uri-and-loop-through-entries-of-that-tag"><code>tag.name</code></a> with <a href="https://craftcms.com/docs/routing" rel="nofollow noreferrer">tag token (routes)</a>, which utilises the original article template for rendering.</li>
<li>Using <a href="https://craftcms.stackexchange.com/questions/14629/tag-mysteriously-not-found"><code>tag.slug</code></a></li>
<li>Using <a href="https://craftcms.stackexchange.com/questions/10344/how-can-i-use-tag-slugs-in-urls"><code>tag.title</code></a> and <code>search()</code></li>
</ol>

<p>I think #2 is the simplest and prettiest solution, but it's relatively undocumented as far as I can tell. Also, I'm using <a href="https://craftcms.com/docs/tags#tag-groups" rel="nofollow noreferrer">two tag groups</a> and, although I've mentioned to my client to not add duplicate tags (e.g. <code>{ 'group 1': 'stress' }</code> <code>{ 'group 2': 'Stress' }</code> I'm interested to know the following:</p>

<ul>
<li>Is #2 the most optimised solution?</li>
<li>Can it be used with a <a href="https://craftcms.com/docs/routing" rel="nofollow noreferrer"><code>tag token</code></a> route?</li>
<li>Will duplicate tags accross tag groups cause conflicts?</li>
</ul>

<p>In my tests using #1 (the urls are ugly in my opinion), <code>Depression</code> seems to return no results, perhaps because there is already a <code>depression</code> tag in another tag group.</p>

<p>Ideally, I want one place for all tags, without having to create two different routes for both tag groups.</p>

<url><routes><tags><routing><slug>
Best solution for articles tag page?
# Best solution for articles tag page?

(Asked by: username_0 on 2016-08-11)

I've been looking through the forum, it seems there's a few methods to retrieve a tags page but I'm looking for the most efficient method (both speed- and template-wise).

1. Using [`tag.name`](https://craftcms.stackexchange.com/questions/828/looking-to-use-a-tag-slug-uri-and-loop-through-entries-of-that-tag) with [tag token (routes)](https://craftcms.com/docs/routing), which utilises the original article template for rendering.
2. Using [`tag.slug`](https://craftcms.stackexchange.com/questions/14629/tag-mysteriously-not-found)
3. Using [`tag.title`](https://craftcms.stackexchange.com/questions/10344/how-can-i-use-tag-slugs-in-urls) and `search()`

I think #2 is the simplest and prettiest solution, but it's relatively undocumented as far as I can tell. Also, I'm using [two tag groups](https://craftcms.com/docs/tags#tag-groups) and, although I've mentioned to my client to not add duplicate tags (e.g. `{ 'group 1': 'stress' }` `{ 'group 2': 'Stress' }` I'm interested to know the following:

* Is #2 the most optimised solution?
* Can it be used with a [`tag token`](https://craftcms.com/docs/routing) route?
* Will duplicate tags accross tag groups cause conflicts?

In my tests using #1 (the urls are ugly in my opinion), `Depression` seems to return no results, perhaps because there is already a `depression` tag in another tag group.

Ideally, I want one place for all tags, without having to create two different routes for both tag groups.

**Tags:** url, routes, tags, routing, slug


---

## 1 answers

### Answer by username_0 on 2016-08-11. Score: 1 | Accepted answer

Ok, so I'm tenatively answering my own question — although I'd like someone to shed light on the duplicate tag problem — it seems like a combination of #1 and #2 works quite nicely, although I haven't extensively tested it yet.
{% if tag is defined %}
  {% set tag = craft.tags.slug(tag).first() %}
  {% set entries = craft.entries.section('articles').relatedTo(tag) %}
{% else %}
  {% set entries = craft.entries.section('articles') %}
{% endif %}

{% for entry in entries %}
  ...
{% endfor %}

This is working with the route articles/tag/[tag token] set up. The only issue is when a tag that has a duplicate slug, whether that be within the same tag group or a different one, it returns no results (I'm not great at reading logs, but it doesn't seem to throw any errors either).
Any additions to this answer would be appreciated!

> **Comments:**
> **username_1**: I believe this would solve your "duplicate tag issue": Change `{% set tag = craft.tags.slug(tag).first() %}` to `{% set tags = craft.tags.slug(tag) %}`, and then do `{% set entries = craft.entries.section('articles').relatedTo(tags.ids()) %}`. With your current code, the `.first()` tag returned might not actually have any related entries, in cases where there are more than one tag with the same name.  
> **username_0**: @MatsMikkelRummelhoff Thanks for the tip, I think for now I'm just going to enforce _no duplicates_ on my client. Yours sounds like a sensible solution, but the idea is to have two distinct tag groups — that method would display all articles, with no easy way of separating them into their groups. Right? I imagine [two routes](https://craftcms.com/docs/routing) would be the only way to achieve this seperation, which is overkill for article tags.  
> **username_0**: You can better see what I mean [here](http://counsellinganywhere.com/articles) and [here](http://counsellinganywhere.com/articles/tag/a-z-of-therapy-types).  
> **username_1**: No disrespect, but the "no duplicates" thing doesn't really sound enforceable. Sooner or later, someone's probably going to add a duplicate tag and you're going to have a problem. IMHO, considering the two tag groups are distinct, it wouldn't be overkill at all to create two routes, it'd make sense. Assuming "Types of issues" and "Types of therapy" are your two tag groups, you could have routes like `articles/therapy/{tag}` and `articles/issues/{tag}` – much more semantic than just `articles/tag/{tag}`, and more future proof (perhaps in the future you'll have 3 or 4 tag groups).  
> **username_1**: Since the two routes would obviously point to the same template, it'd be a minimal amount of work to set up, too – all you really need is to add a `.group(tagGroupHandle)` to your `craft.tags` query, where you pull the `tagGroupHandle` from the request URI (either by using a route token, or by looking at the relevant segment via [`craft.request`](https://craftcms.com/classreference/variables/HttpRequestVariable)).  

---

