<blockquote>
  <p><code>{% if not entries %}</code> never executes, even if i enter complete nonsense in the url.</p>
</blockquote>

<p>You’re not ensuring that <code>segmentCategory</code> is a valid category slug anywhere. If it’s not, then <code>category</code> will get set to <code>null</code>, which will basically prevent it from affecting the following <code>relatedTo</code> param.</p>

<p>So right after grabbing the <code>category</code> you should make sure it exists;</p>

<pre><code>{% set category = craft.categories.slug(segmentCategory).first() %}
{% if not category %}
  {% exit 404 %}
{% endif %}
</code></pre>

<blockquote>
  <p>the array entries is never outputs anything</p>
</blockquote>

<p>I think this is because your <code>relatedTo</code> parameter isn’t set up correctly. If the used/new categories are being assigned to the entries via a Categories field on the entry, then the category should be set to the <code>targetElement</code> property, not <code>sourceElement</code>, since the category is the <em>target</em> element of the relation. And you can just get rid of the current <code>targetElement</code> stuff, which should have been <code>sourceElement</code>, but is redundant and doesn’t need to be specified anyway.</p>

<pre><code>{% set entries = craft.entries({
    type: segmentEntryType,
    relatedTo: { targetElement: category }
}) %}
</code></pre>

<blockquote>
  <p>Ok, now i get the posts, but if i pass an invalid string to the segmentEntryType the template still loads.</p>
</blockquote>

<p>This conditional:</p>

<pre><code>{% if not entries %}
</code></pre>

<p>should be:</p>

<pre><code>{% if not entries|length %}
</code></pre>

<p>(At this point in your template, <code>entries</code> will be a defined ElementCriteriaModel regardless of whether it matches any entries. Including the <code>length</code> filter in your conditional will force it to execute the DB query and return the actual results.)</p>

CC BY-SA 3.0
2015-08-05T17:19:06.857Z
Ok, now i get the posts, but if i pass an invalid string to the `segmentEntryType` the template still loads.
Samuel E.
CC BY-SA 3.0
2015-08-06T13:15:11.370Z
@SamuelElgozi Just updated my answer to address that.
Brandon Kelly
CC BY-SA 3.0
2015-08-06T14:54:48.123Z
First thank you, and that does redirect me to the 404 page. the problem is that even if the entry type exists but has no entries i get redirected. (sorry for making it complicated), i've opened [another question for that](http://craftcms.stackexchange.com/questions/10872/checking-if-a-value-matches-an-entry-type-handle).
Samuel E.
CC BY-SA 3.0
2015-08-05T15:06:00.790Z
2015-08-06T13:14:56.727Z
2015-08-06T13:14:56.727Z
Brandon Kelly
<p>I'll explain a bit of the structure of the content and then proceed to the issue.</p>

<p>So i have a website with a shop in it with the url mysite.com/shop/,
it is an channel section, and its template is located at craft/templates/shop/index.html.</p>

<p>The shop is divided in to 2 categories - "new", and "used",
the url for each is mysite.com/shop/(used or new), and its template is located at craft/templates/shop/category.html.</p>

<p>The section(channel) shop that is mentioned above also has multiple entry types, for example "televisions", "consoles", "computers"... and each one has a required category field to chose one of the above(new or used), and the url for those entries is 'shop/{condition.first().slug}/{type}/{slug}'
(the condition field is the category).</p>

<p>Now to what i'm trying to do, and whats the issue.
I want the user to be able to filter through entry types within the category template. what i mean is that te used could click "show only tv's"
and a url with the new filter will load but with the same template, so the url would look like that:
mysite.com/shop/used/television/</p>

<p>To accomplish that i've made a new dynamic route by adding the nex line to my routes.php:
<code>'shop/(?P&lt;segmentCategory&gt;(new|used))/(?P&lt;segmentEntryType&gt;[a-z]+)' =&gt; 'shop/category',</code></p>

<p>And the template goes like this(the twig part only):</p>

<pre><code>{% if segmentCategory is defined %}
    {% set category = craft.categories.slug(segmentCategory).first() %}
    {% set entries = craft.entries({
        type: segmentEntryType,
        relatedTo: ['and',
            { sourceElement: category },
            { targetElement: craft.entries.type(segmentEntryType).status(null).first }
        ]
    }) %}
    {% if not entries %}
        {% exit 404 %}
    {% endif %}
{% else %}
    {% set entries = craft.entries.relatedTo(category) %}
{% endif %}

{% for entry in entries %}
    {{ entry.title }}
{% else %}
    There are no entries matching you criteria right no, please try again in a few days.
{% endfor %}
</code></pre>

<p>I'm having two issues with this code.</p>

<p><strong>First:</strong> <code>{% if not entries %}</code> never executes, even if i enter complete nonsense in the url.
<strong>Second:</strong> the array <code>entries</code> is never outputs anything...</p>

<p>Thanks ahead!</p>

<categories><routes><entry-type><forloop><entrymodel>
Looping through returns nothing after using dynamic routes to get the entry type and category
# Looping through returns nothing after using dynamic routes to get the entry type and category

(Asked by: username_0 on 2015-08-05)

I'll explain a bit of the structure of the content and then proceed to the issue.
So i have a website with a shop in it with the url mysite.com/shop/,
it is an channel section, and its template is located at craft/templates/shop/index.html.
The shop is divided in to 2 categories - "new", and "used",
the url for each is mysite.com/shop/(used or new), and its template is located at craft/templates/shop/category.html.
The section(channel) shop that is mentioned above also has multiple entry types, for example "televisions", "consoles", "computers"... and each one has a required category field to chose one of the above(new or used), and the url for those entries is 'shop/{condition.first().slug}/{type}/{slug}'
(the condition field is the category).
Now to what i'm trying to do, and whats the issue.
I want the user to be able to filter through entry types within the category template. what i mean is that te used could click "show only tv's"
and a url with the new filter will load but with the same template, so the url would look like that:
mysite.com/shop/used/television/
To accomplish that i've made a new dynamic route by adding the nex line to my routes.php:
'shop/(?P(new|used))/(?P[a-z]+)' => 'shop/category',
And the template goes like this(the twig part only):
{% if segmentCategory is defined %}
    {% set category = craft.categories.slug(segmentCategory).first() %}
    {% set entries = craft.entries({
        type: segmentEntryType,
        relatedTo: ['and',
            { sourceElement: category },
            { targetElement: craft.entries.type(segmentEntryType).status(null).first }
        ]
    }) %}
    {% if not entries %}
        {% exit 404 %}
    {% endif %}
{% else %}
    {% set entries = craft.entries.relatedTo(category) %}
{% endif %}

{% for entry in entries %}
    {{ entry.title }}
{% else %}
    There are no entries matching you criteria right no, please try again in a few days.
{% endfor %}

I'm having two issues with this code.
First: {% if not entries %} never executes, even if i enter complete nonsense in the url.
Second: the array entries is never outputs anything...
Thanks ahead!

**Tags:** categories, routes, entry-type, forloop, entrymodel


---

## 1 answers

### Answer by username_1 on 2015-08-05. Score: 4

{% if not entries %} never executes, even if i enter complete nonsense in the url.

You’re not ensuring that segmentCategory is a valid category slug anywhere. If it’s not, then category will get set to null, which will basically prevent it from affecting the following relatedTo param.
So right after grabbing the category you should make sure it exists;
{% set category = craft.categories.slug(segmentCategory).first() %}
{% if not category %}
  {% exit 404 %}
{% endif %}


the array entries is never outputs anything

I think this is because your relatedTo parameter isn’t set up correctly. If the used/new categories are being assigned to the entries via a Categories field on the entry, then the category should be set to the targetElement property, not sourceElement, since the category is the target element of the relation. And you can just get rid of the current targetElement stuff, which should have been sourceElement, but is redundant and doesn’t need to be specified anyway.
{% set entries = craft.entries({
    type: segmentEntryType,
    relatedTo: { targetElement: category }
}) %}


Ok, now i get the posts, but if i pass an invalid string to the segmentEntryType the template still loads.

This conditional:
{% if not entries %}

should be:
{% if not entries|length %}

(At this point in your template, entries will be a defined ElementCriteriaModel regardless of whether it matches any entries. Including the length filter in your conditional will force it to execute the DB query and return the actual results.)

> **Comments:**
> **username_0**: Ok, now i get the posts, but if i pass an invalid string to the `segmentEntryType` the template still loads.  
> **username_1**: @SamuelElgozi Just updated my answer to address that.  
> **username_0**: First thank you, and that does redirect me to the 404 page. the problem is that even if the entry type exists but has no entries i get redirected. (sorry for making it complicated), i've opened [another question for that](http://craftcms.stackexchange.com/questions/10872/checking-if-a-value-matches-an-entry-type-handle).  

---

