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

Add Collection method to merge with another page collection, and a Collection filter for both non-modular and modular children #1258

Closed
hughbris opened this issue Jan 19, 2017 · 2 comments

Comments

@hughbris
Copy link
Contributor

Both of these enhancements would solve my immediate problem, but each is also useful in its own right IMHO.

My problem is (was) that I wanted to incorporate some modular items (which I tagged 'visible' in frontmatter) into my navigation menu (as hash fragment links, what some people call jumplinks). So, since the collection filters treat modular and non-modular children mutually exclusively, I thought I'd just merge one into the other. (Twig's merge filter produces an array result.)

Firstly, if I'd been able to grab both types in a single filter, the problem would not have arisen.

Secondly, there doesn't seem to be a nifty way to combine two Grav page collections. Thanks to @mahagr I came up with a kludgy way:

{% set menu_pages = page.children.visible %}
{% for frag in page.children.modular if frag.header.visible %}
    {% set menu_pages = menu_pages.addPage(frag) %}
{% endfor %}
{# there may also be a more concise way to do this using page.evaluate()
   but we couldn't figure that out #}

but I think it should be a standard method in PHP and exposed to Twig :)

rhukster added a commit that referenced this issue Jan 20, 2017
@rhukster
Copy link
Member

So i just added a simple merge method:

{% set collection1 = page.collection %}
{% set collection2 = page.collection('test_content') %}

<h3>Merged</h3>
<ul>
{% for p in collection1.merge(collection2) %}
<li>{{ p.title }}</li>
{% endfor %}
</ul>

@hughbris
Copy link
Contributor Author

Thanks :)

I think it's still worth considering an "all children" filter down the track, but this solves my problem in that I can undo my messy workaround. Loving this kind of responsiveness to issues as I build sites, can usually get the solution through core updates within a few days :)

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

No branches or pull requests

2 participants