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 unfold #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Add unfold #9

wants to merge 5 commits into from

Conversation

tsnorri
Copy link

@tsnorri tsnorri commented May 28, 2018

No description provided.

@dpolac
Copy link
Owner

dpolac commented Jun 10, 2018

Hello @tsnorri, thanks for your contribution!
Calculating Fibonacci numbers is a quite abstract example. Especially since most calculations in web apps are not done in templates, but rather in controller.
Could you please provide a more down-to-earth example of use?

@tsnorri
Copy link
Author

tsnorri commented Feb 28, 2019

I just noticed your question, sorry about the rather long delay. A more down-to-earth use case would be traversing a page hierarchy from a non-root towards the root, i.e. by using parent() or similar. This would be useful in e.g. creating a breadcrumb list. I don’t have sample code right now but I can try to produce something.

@tsnorri
Copy link
Author

tsnorri commented Feb 28, 2019

Something like this:

{% set breadcrumbs = page|unfold(=> (_ is null or _.root) ? null : [_, _.parent])|reverse %}

@vendethiel
Copy link

Why do you need the _.root check? Isn’t your root’s parent null?

@tsnorri
Copy link
Author

tsnorri commented Mar 1, 2019

Why do you need the _.root check? Isn’t your root’s parent null?

Apparently in Grav (at least version 1.4.5) the root isn’t actually a page but a directory. The directory may contain other pages in addition to the designated home page.

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

Successfully merging this pull request may close these issues.

4 participants