twig-lambda icon indicating copy to clipboard operation
twig-lambda copied to clipboard

Add unfold

Open tsnorri opened this issue 7 years ago • 5 comments

tsnorri avatar May 28 '18 22:05 tsnorri

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?

dpolac avatar Jun 10 '18 20:06 dpolac

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 avatar Feb 28 '19 14:02 tsnorri

Something like this:

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

tsnorri avatar Feb 28 '19 22:02 tsnorri

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

vendethiel avatar Feb 28 '19 22:02 vendethiel

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.

tsnorri avatar Mar 01 '19 13:03 tsnorri