<p>Try this:</p>

<pre><code>{% set category = craft.categories(segmentCategory).first %}
</code></pre>

<p>Without the <code>first</code> at the end, you're setting <code>category</code> to an ElementCriteriaModel. Which would be fine in many cases, but it's not an <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">acceptable variable type</a> for <code>targetElement</code>.</p>

<p>Ditto for the <code>sourceElement</code>:</p>

<pre><code>craft.entries.section(segmentEntryType).status(null).first
</code></pre>

CC BY-SA 3.0
2015-08-05T09:37:32.260Z
It fixed the error but now for some reason it doesn't filter the content... and the conditional `{% if not entries %}` never returns true
Samuel E.
CC BY-SA 3.0
2015-08-05T16:57:15.260Z
Great! I'd say to post that as a separate question, but it looks like you [already have...](http://craftcms.stackexchange.com/q/10859/45) :)
Lindsey D
CC BY-SA 3.0
2015-08-05T04:42:33.927Z
2015-08-05T04:42:33.927Z
Lindsey D
<p>So i'm trying to pass the variables from a dynamic route to a template and then to use those variables to filter trough entries.
the catch is that the same template is used to display an "organic"(by organic i mean that it is set up from the CP) category as well.</p>

<p>This is the routes.php</p>

<pre><code>'shop/(?P&lt;segmentCategory&gt;(new|used))/(?P&lt;segmentEntryType&gt;[a-z]+)' =&gt; 'shop/category',
</code></pre>

<p>And this is from the template:</p>

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

<p>The error:</p>

<pre><code>    Object of class Craft\ElementCriteriaModel could not be converted to string
</code></pre>

<p>craft/templates/shop/category.html(47)</p>

<pre><code>47 {% extends "_layout" %} ||||||||||||||(Highlighted)||||||||||||||||
48 {% block content %}
</code></pre>

<filter><entry-type><routing><error-templates>
Error while trying to use dynamic routing to pass variables to the template and using them
# Error while trying to use dynamic routing to pass variables to the template and using them

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

So i'm trying to pass the variables from a dynamic route to a template and then to use those variables to filter trough entries.
the catch is that the same template is used to display an "organic"(by organic i mean that it is set up from the CP) category as well.
This is the routes.php
'shop/(?P(new|used))/(?P[a-z]+)' => 'shop/category',

And this is from the template:
{% if segmentCategory is defined %}
    {% set category = craft.categories(segmentCategory) %}
    {% set games = craft.entries({
        type: segmentEntryType,
        relatedTo: ['and',
            { sourceElement: craft.entries.section(segmentEntryType).status(null) },
            { targetElement: category }
        ]
    }) %}
{% else %}
    {% set entries = craft.entries.relatedTo(category) %}
{% endif %}

The error:
    Object of class Craft\ElementCriteriaModel could not be converted to string

craft/templates/shop/category.html(47)
47 {% extends "_layout" %} ||||||||||||||(Highlighted)||||||||||||||||
48 {% block content %}

**Tags:** filter, entry-type, routing, error-templates

---

## 1 answers

### Answer by username_1 on 2015-08-05. Score: 3 | Accepted answer

Try this:
{% set category = craft.categories(segmentCategory).first %}

Without the first at the end, you're setting category to an ElementCriteriaModel. Which would be fine in many cases, but it's not an acceptable variable type for targetElement.
Ditto for the sourceElement:
craft.entries.section(segmentEntryType).status(null).first

> **Comments:**
> **username_0**: It fixed the error but now for some reason it doesn't filter the content... and the conditional `{% if not entries %}` never returns true  
> **username_1**: Great! I'd say to post that as a separate question, but it looks like you [already have...](http://craftcms.stackexchange.com/q/10859/45) :)  

---

