Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Page collections based on page.header components

Started by Muut Archive 10 years ago · 3 replies · 620 views
10 years ago

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.

10 years ago

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':

YAML
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:

TWIG
{% if page.header.color != "green" %}
    The color is not green. Instead it is {{ page.header.color }}. 
{% endif %}
10 years ago

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]
    YAML
    
    Someting like this (i know its very wrong - just writing down to explain better):
    ---html
    content:
    items:
    
  • '@page.children': '/xyz'
  • '@page.header':
    color: [red]
    TXT
    
    Cheers, sbstn
    
10 years ago

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.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago