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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Move collection definition from page.header to config file

Started by Maciej 5 years ago · 4 replies · 390 views
5 years ago

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?

YAML
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 https://learn.getgrav.org/16/basics/grav-configuration#config-variable-namespacing

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.

last edited 02/10/21 by pamtbaau
5 years ago

@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:
    YAML
    siblings:
    items: '@self.siblings'
    order:
      by: date
      dir: desc
    limit: 10
    
  • Added the following snippet to 'user/themes/quark/templates/default.html.twig'

    TWIG
    {% set options = config.collections.siblings %}
    {% set siblings = page.collection(options) %}
    
    {% for sibling in siblings %}
    <p>{{sibling.title }}</p>
    {% endfor %}
    

    Result:

  • Page 'http://localhost/' printed 'Typography'
  • Page 'http://localhost/typography printed 'Home'
👍 2
5 years ago

Dear @pamtbaau is works somehow great but...

When I place this code:

TWIG
{% 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

TWIG
{% 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?

last edited 02/11/21 by pamtbaau
5 years ago

@mkukulka, What means:

it doesn’t work.

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

5 years ago

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

YAML
---

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

YAML
---

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

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 %}
last edited 02/12/21 by Maciej

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 18 hours ago
Support · by Justin Young, 18 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago