Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

How to use Collection::ofType

Started by ontime 9 years ago · 6 replies · 787 views
9 years ago

Hi,
I can't make ofType or ofOneOfTheseTypes work.
Let say I have the following code:

TWIG
content:
    items: '@self.modular'

{% for tile in page.collection().ofType('home_introduction') %}
    {{ tile.content }}
{% endfor %}

The result is empty.
Why doesn't it work? When I take out ofType(), everything is fine.
But I need to filter the collection to add a new modular content into the page.

Also what I ultimately want to achieve is something like this, with ofOneOfTheseTypes, but my syntax seems wrong:

TWIG
{% for tile in page.collection().ofOneOfTheseTypes({'home_introduction', 'home_item'}) %}
    {{ tile.content }}
{% endfor %}
last edited 06/30/17 by ontime
9 years ago

@ontime:
{% for tile in page.collection().ofOneOfTheseTypes({'home_introduction', 'home_item'}) %}
{{ tile.content }}
{% endfor %}

Collection::ofOneOfTheseTypes() accepts an Array. You're passing a (malformed) object. Try

TWIG
{% for tile in page.collection().ofOneOfTheseTypes(['home_introduction', 'home_item']) %}
9 years ago

The first example seems fine. What's your page structure? (a screenshot is fine)

9 years ago

Thanks for the array syntax tip, I am new to twig.
With the right syntax, now both methods give me an empty result.

Here is my templates structure. Is this what you meant ?

<img src="//cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex036/uploads/getgrav/original/1X/40c20d85bd807f12bdc113031fe6a689936789b9.png" width="303" height="396">

9 years ago

Looks right. If your page has a home_introduction.md modular child page, it should show up.

Is it in a folder starting with an _ (underscore)? Can you paste the page structure as well?

9 years ago

Yes, the folders of the modular pages are all prefixed with an _.

Here is the structure of the page:

<img src="//cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex036/uploads/getgrav/original/1X/6b61ae9fe6abb27647f3ae41004c3318680eed5f.png" width="431" height="203">

last edited 07/06/17 by ontime
9 years ago

PM me your user/pages folder, I'll check directly

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 54 12 hours ago
Support · by Anna, 3 days ago
2 60 15 hours ago
Support · by Justin Young, 15 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago