Move collection definition from page.header to config file

Hi,

I wanto to move Yaml header from page.header to config.file-name. Is it possible to all elements? It works for me with image but what about collections?

content:
  items: '@self.siblings'
  order:
    by: date
    dir: desc
  limit: 10

aktualnosci:
  items:
    '@page.children': /products/polish-functionality/news
  order:
    by: date
    dir: desc
  limit: 10

file_browser:
  source: 'user://downloads/release/plf/'

submenu:
  items:
    '@page.children': /products/polish-functionality
  order:
    by: folder
    dir: asc
  limit: 10

talking about his section in docs Configuration | Grav Documentation

Config Variable Namespacing

Paths to the configuration files will be used as a namespace for your configuration options.

Alternatively, you can put all the options into one file and use YAML structures to specify the hierarchy for your configuration options. This namespacing is built from a combination of the path + filename + option name .

For example: An option such as author: Frank Smith in file plugins/myplugin.yaml could be accessible via: plugins.myplugin.author . However, you could also have a plugins.yaml file and in that file have an option name called myplugin: author: Frank Smith and it would still be reachable by the same plugins.myplugin.author namespace.

@mkukulka, You can create collections not only in the frontmatter of a page, but also dynamically in PHP and Twig.

Here is one example…

  • Fresh Grav 1.7.5 using Quark
  • Created file ‘/user/config/collections.yaml’ with the following content:
    siblings:
      items: '@self.siblings'
      order:
        by: date
        dir: desc
      limit: 10
    
  • Added the following snippet to ‘user/themes/quark/templates/default.html.twig’
    {% set options = config.collections.siblings %}
    {% set siblings = page.collection(options) %}
    
    {% for sibling in siblings %}
      <p>{{sibling.title }}</p>
    {% endfor %}
    

Result:

2 Likes

Dear @pamtbaau is works somehow great but…

When I place this code:

{% set options = config.pf.content %}
{% set contents = page.collection(options) %}

{% set options = config.pf.news %}
{% set newss = page.collection(options) %}

{% for news in newss %}
  <p>{{news.title }}</p>
{% endfor %}

{% for content in contents %}
  <p>{{content.title }}</p>
{% endfor %}

to file default.md it all works correct. But when i transfer

{% set options = config.pf.content %}
{% set contents = page.collection(options) %}

{% set options = config.pf.news %}
{% set newss = page.collection(options) %}

to default.html.twig it doesn’t work.
What am I missing?

@mkukulka, What means:

it doesn’t work.

My example shows the snippet in ‘user/themes/quark/templates/default.html.twig’ and works fine.

I see that you placed “for” function also in default.html.twig. But I need sth more flexible and don’t know if it is possible.

When I have this configuration in code it works fine:

default.pl.md

---

title: 'Polish Functionality - Starter Pack'
process:
    markdown: true
    twig: true
servicedesk_email: XXX
www: 'XXX'
docs: 'XXX'
appsource: 'XXX'
product_logo: /user/files/logo/polish-functionality.svg
content:
    items: '@self.siblings'
    order:
        by: date
        dir: desc
    limit: 10
aktualnosci:
    items:
        '@page.children': /products/polish-functionality/news
    order:
        by: date
        dir: desc
    limit: 10
file_browser:
    source: 'user://downloads/release/plf/'
submenu:
    items:
        '@page.children': /products/polish-functionality
    order:
        by: folder
        dir: asc
    limit: 10

---

{% set options = config.pf.content %}
{% set contents = page.collection(options) %}

{% set options = config.pf.news %}
{% set newss = page.collection(options) %}

{% for news in newss %}
  <p>{{news.title }}</p>
{% endfor %}

{% for content in contents %}
  <p>{{content.title }}</p>
{% endfor %}

but when I have two files is doesn’t works.
first: default.pl.md

---

title: 'Polish Functionality - Starter Pack'
process:
    markdown: true
    twig: true
servicedesk_email: XXX
www: 'XXX'
docs: 'XXX'
appsource: 'XXX'
product_logo: /user/files/logo/polish-functionality.svg
content:
    items: '@self.siblings'
    order:
        by: date
        dir: desc
    limit: 10
aktualnosci:
    items:
        '@page.children': /products/polish-functionality/news
    order:
        by: date
        dir: desc
    limit: 10
file_browser:
    source: 'user://downloads/release/plf/'
submenu:
    items:
        '@page.children': /products/polish-functionality
    order:
        by: folder
        dir: asc
    limit: 10

---


{% for news in newss %}
  <p>{{news.title }}</p>
{% endfor %}

{% for content in contents %}
  <p>{{content.title }}</p>
{% endfor %}

and second: defaut.html.twig

{% extends "partials/page.html.twig" %}

{% set options = config.pf.content %}
{% set contents = page.collection(options) %}

{% set options = config.pf.news %}
{% set newss = page.collection(options) %}

{% block content %}
    {{ page.content|raw }}
{% endblock %}