Hey all,
i wonder if its possible generate page collections based on page.header components (not taxonimies as its too limited for some needs).
As simple example:
A bunch of pages in folder ‘x’ with page.header.color with select-options ‘green’ and ‘red’.
At a new page the output should be: all pages in folder ‘x’ with page.header.color != green
Is it possible in general as Grav strongly focuses on the taxonomies?
Thanks for help.
Though I don’t fully understand your question maybe this hint will help.
You can set your own custom variables in a page’s header frontmatter. For example a variable named ‘color’:
title: My Red Page
color: red
published: true
Then possibly in a modular Twig template determine what to do based on the value of the variable ‘color’. Perhaps something like:
{% if page.header.color != "green" %}
The color is not green. Instead it is {{ page.header.color }}.
{% endif %}
Hey bleutzinn,
thanks for your respond and sorry for not describing my issue clear enough - i will work to improve that.
Anyway, your solution sure enough works and was so easy i had to clap my forehead…
The one “problem” i see is that filtering through twig could probably result in a lot of templates - manageable, but not nice.
I hoped, due to the awesome flexibility of grav, there is a solution to set such filters already within the header of the page (using just one template), like its possible with taxonimies:
—html
content:
items:
- ‘@page.children’: ‘/xyz’
- ‘@taxonomy’:
category: [red]
Someting like this (i know its very wrong - just writing down to explain better):
---html
content:
items:
- '@page.children': '/xyz'
- '@page.header':
color: [red]
Cheers, sbstn
As I haven’t used page collections I realise I’m on dangerous ground here but I’m sure there is no need for many Twig templates.
By default Grav looks for a Twig template file based on the page file name. A new page by default is named default.md
and Grav looks for the template default.html.twig
. You can also specify which Twig template to use per page by includingtemplate: my_custom_template
in the page header frontmatter.
In this case Grav will use the Twig template called my_custom_template.html.twig
.
So it is very well possible to just use a single Twig template for many different pages. I hope this helps you move on.