Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to filter collection on page type #792

Closed
fvsch opened this issue Apr 17, 2016 · 3 comments
Closed

Ability to filter collection on page type #792

fvsch opened this issue Apr 17, 2016 · 3 comments

Comments

@fvsch
Copy link

fvsch commented Apr 17, 2016

I couldn’t find this in the documentation, forum or even the code (looking in Grav/Common/Page/Page.php). It seems Grav cannot filter a collection by page type.

For instance I want all descendant pages which have a article.md type:

content:
    items:
        '@self.descendants'

I can filter results on dates, I can order and limit results, but more filtering options would be nice.

In templates I can use page.collection.ofType('article'), but only in the template and it doesn't affect plugins who are using the page’s collection directly.

@rhukster
Copy link
Member

Again this sort of relates to your previous collection question...

Page collections are configured in YAML as you know. We have a lot of collection options - https://learn.getgrav.org/content/collections#summary-of-collection-options

However, we don't have a lot of filtering options. We support filtering via taxonomy as the primary method of filtering in these page collections. One of the reasons for this is that defining filters is not trivial in YAML, while it is much easier to do in PHP or Twig (where we have logic functions).

Because of these the best approach is to gather a rather general collection based from the page, and then filter it as you loop over it in PHP or Twig.

I understand what you are saying about the potential issues this causes with having to filter this in theme/plugins, but there is a simple technique to mitigate this. You can simply filter the collection in a higher level twig template, and then pass this filtered collection on to plugins and other partials.

Another option is to leverage taxonomy to achieve your goals. For example you could set a taxonomy type of article in your pages that you are trying to filter.

A potential solution down the road, is for us to augment the simple YAML syntax with a much more powerful query language syntax (similar to SQL). This is something I considered originally but thought that it would be better to go with something simpler to start.

@fvsch
Copy link
Author

fvsch commented Apr 18, 2016

Thanks for the feedback. :)

@guategeek
Copy link

So your saying that the filtering of a collection can be done in the twig rather than in creating the collection itself?

I'm trying to do some very specific template stuff with a modular page, I want some modules placed in different portions of the template (contained in different parent for styling purposes) but I'm having a heck of a time figuring out how. If you could give me an example of a for loop that filters based on taxonomy (named "section") that would really help.

This is the for loop I have right now
{% for module in page.collection() %} <!-- <div id="{{ _self.pageLinkName(module.menu) }}"></div> --> {{ module.content }} {% endfor %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants