Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-05T09:37:32.260Z
Answers.Text: It fixed the error but now for some reason it doesn't filter the content... and the conditional `{% if not entries %}` never returns true
Answers.UserDisplayName: Samuel E.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-05T16:57:15.260Z
Answers.Text: 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) :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-05T04:42:33.927Z
Answers.LastActivityDate: 2015-08-05T04:42:33.927Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Tags: <filter><entry-type><routing><error-templates>
Title: Error while trying to use dynamic routing to pass variables to the template and using them
ThreadText: # 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<segmentCategory>(new|used))/(?P<segmentEntryType>[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](http://buildwithcraft.com/docs/relations#the-relatedTo-param) 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) :)  
  
  ---
  
  