<p>While Marion's post is right, it's not quite what I was trying to achieve I didn't want to have 2 templates (although use of macros is perfectly suitable).</p>

<p>What I've done to get around this is is add a <code>tag</code> segment for tags and leave categories as is. Then I added a custom route to support the tag segement but use the same template code </p>

<p><strong>URI's:</strong></p>

<p><code>/news/*category*</code></p>

<p><code>/news/tag/*tag*</code></p>

<p>The category route is defined automagically, but I needed to set up a route for the <code>tag</code> view.</p>

<p><strong>Template Path:</strong></p>

<p><code>news/index</code> (which is also defined as my Category Template in the category settings)</p>

<p>Using the same template code above, I'm able to get the index/list view of a <code>category</code> and <code>tag</code> when they are the same all within the same template.</p>

CC BY-SA 3.0
2014-07-18T16:06:59.060Z
2014-07-18T16:06:59.060Z
Ryan Shrum
<p>If the url for posts with the <strong>category</strong> <code>design</code> is <code>.com/news/design</code> and the url for posts with the <strong>tag</strong> <code>design</code> is also <code>.com/news/design</code>, then you are not going to be able to see both lists. You do need that extra segment in the url.</p>

<p>What I do to stay DRY is let Craft route tags and categories to different templates (so you don't have to repeat the <em>is this a category or a tag</em> logic). Then on the different templates, use a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">macro</a> or <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow">include</a> for the common code.</p>

<p>Here is what the categories page looks like with a macro:</p>

<pre><code>{% extends '_layout' %}
{% import '_macros/_news' as news %}

{% set newsPosts = craft.entries.section('news')relatedTo(category).limit(5) %}

{% block content %}
  &lt;h1&gt;Entries with category {{category.title}}&lt;/h1&gt;
  {{ news.index(newsPosts) }} {# all the shared code is in news.index() #}
{% endblock content %}
</code></pre>

CC BY-SA 3.0
2014-07-16T16:50:24.893Z
2014-07-16T16:50:24.893Z
Marion Newlevant
<p>I'm trying to stay as DRY as possible here and sharing one template for a news list view; all, category and tag all sharing the same template (and trying to refrain from having a category or tag segment). My query isn't returning any results when the segment 2 (in this case the url encoded tag or category slug) when there is a tag and category that share the same name. Example, 'design' is a category for design related posts; 'design' is a tag for posts that featured a design service within the 'portfolio' category. My template code is below. Is there a work around here or do I need to have a second segment for tag/category (i.e. <code>.com/news/category/design</code> and <code>.com/news/tag/design</code> - or one or the other to differentialte)</p>

<pre><code>{% if category is defined %}

    {% set newsPosts = craft.entries.section('news').relatedTo(category).limit(5) %}

{% elseif tag is defined %}

    {% set tag = craft.tags.groupId(3).name(tag).first() %}

    {% set newsPosts = craft.entries.section('news').relatedTo(tag).limit(5) %}

{% else %}

    {% set newsPosts = craft.entries.section('news').limit(5) %}

{% endif %}
</code></pre>

<entries><templating><tags><categories>
Sharing a template with identical category and tag name
# Sharing a template with identical category and tag name

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

I'm trying to stay as DRY as possible here and sharing one template for a news list view; all, category and tag all sharing the same template (and trying to refrain from having a category or tag segment). My query isn't returning any results when the segment 2 (in this case the url encoded tag or category slug) when there is a tag and category that share the same name. Example, 'design' is a category for design related posts; 'design' is a tag for posts that featured a design service within the 'portfolio' category. My template code is below. Is there a work around here or do I need to have a second segment for tag/category (i.e. .com/news/category/design and .com/news/tag/design - or one or the other to differentialte)
{% if category is defined %}

    {% set newsPosts = craft.entries.section('news').relatedTo(category).limit(5) %}

{% elseif tag is defined %}

    {% set tag = craft.tags.groupId(3).name(tag).first() %}

    {% set newsPosts = craft.entries.section('news').relatedTo(tag).limit(5) %}

{% else %}

    {% set newsPosts = craft.entries.section('news').limit(5) %}

{% endif %}

**Tags:** entries, templating, tags, categories

---

## 2 answers

### Answer by username_0 on 2014-07-18. Score: 2 | Accepted answer

While Marion's post is right, it's not quite what I was trying to achieve I didn't want to have 2 templates (although use of macros is perfectly suitable).

What I've done to get around this is is add a `tag` segment for tags and leave categories as is. Then I added a custom route to support the tag segement but use the same template code

**URI's:**

`/news/*category*`

`/news/tag/*tag*`

The category route is defined automagically, but I needed to set up a route for the `tag` view.

**Template Path:**

`news/index` (which is also defined as my Category Template in the category settings)

Using the same template code above, I'm able to get the index/list view of a `category` and `tag` when they are the same all within the same template.


---

### Answer by username_1 on 2014-07-16. Score: 2

If the url for posts with the category design is .com/news/design and the url for posts with the tag design is also .com/news/design, then you are not going to be able to see both lists. You do need that extra segment in the url.
What I do to stay DRY is let Craft route tags and categories to different templates (so you don't have to repeat the is this a category or a tag logic). Then on the different templates, use a macro or include for the common code.
Here is what the categories page looks like with a macro:
{% extends '_layout' %}
{% import '_macros/_news' as news %}

{% set newsPosts = craft.entries.section('news')relatedTo(category).limit(5) %}

{% block content %}
  Entries with category {{category.title}}
  {{ news.index(newsPosts) }} {# all the shared code is in news.index() #}
{% endblock content %}


---

