Looping through returns nothing after using dynamic routes to get the entry type and category 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&lt;segmentCategory&gt;(new|used))/(?P&lt;segmentEntryType&gt;[a-z]+)' =&gt; '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!
{% 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.)