0

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 %}
3

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
2
  • 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.
    Aug 5 '15 at 9:37
  • Great! I'd say to post that as a separate question, but it looks like you already have... :)
    – Lindsey D
    Aug 5 '15 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.