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

Modular page with different collections

first-time

Solved by pamtbaau View solution

Started by Kernel 3 years ago · 4 replies · 685 views
3 years ago

I am trying to set a page that can show the first item of each of the 3 categories I am planning to use in my website.

I thought of creating a modular page, and to use 3 different modules to create these collections and get the latest page on each of my categories.

But I don't get to achieve this.

first of all, is this possible?
second, what type should the module be?
I created a modular.md on the root, and for each of the modules, a folder with a .md file inside, but I am not getting anything.

at a second step I thought it might have to do with the template,

I am using the quark theme (ootb).
I thought of creating a new file under modular folder (something like article.html.twig) do try if that is a possible solution, but this type does not appear as an option to be selected.

So I am clearly doing everything wrong 😅

any advice?
thanks!

3 years ago Solution

@kernel52,

Try the following:

  • Download skeleton Blog Site which contains pages with several tags.
  • Create new page /user/pages/02.test/test.md
  • Create 3 collections in page

    YAML
    ---
    mushroom:
    items:
      '@taxonomy.tag': mushroom
    limit: 1
    order:
        by: date
        dir: desc
    
    travel:
    items:
      '@taxonomy.tag': travel
    limit: 1
    order:
        by: date
        dir: desc
    
    architecture:
    items:
      '@taxonomy.tag': architecture
    limit: 1
    order:
        by: date
        dir: desc
    ---
    
    # My page with last items of 3 categories
    
  • Create new template /user/themes/quark/templates/test.html.twig with the following content:

    TWIG
    {% extends 'partials/base.html.twig' %}
    
    {% block content %}
    
    {{ page.content|raw}}
    
    <h2>Looping 3 collections</h2>
    
    {% for child in page.collection('mushroom') %}
      {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
    {% endfor %}
    
    {% for child in page.collection('travel') %}
      {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
    {% endfor %}
    
    {% for child in page.collection('architecture') %}
      {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
    {% endfor %}
    
    <h2>Or merge three collections into 1</h2>
    
    {% set collection = page.collection('mushroom').merge(page.collection('travel')).merge(page.collection('architecture')) %}
    
    {% for child in collection %}
      {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
    {% endfor %}
    
    {% endblock %}
    
  • Browse to page http://yourdomain/test
    It will show you two lists of pages, where each list contains the last item of each 'tag'.

Note:

  • When merging collections, the result will be deduped. Meaning: If a page has both tag 'mushroom' and 'travel', the page will be shown only once.
👍 1
3 years ago

thanks pamtbaau!
I am very grateful for the detailed explanation you provided.

I'm giving it a try and will get back to you to (hopefully) confirm that it is as perfect as it looks 🙂

3 years ago

@kernel52, Have you had the chance to experiment with the collections?

3 years ago

Hi pamtbaau,

I did manage to test. It works as a charm!
I changed the items to be the liat of items under a page and got exactly what I wanted!

I now have to learn how to work with the templates to import my old drupal template into grav 😅

Any hints are more than welcome 🙏

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 66 18 hours ago
Support · by Anna, 3 days ago
2 72 21 hours ago
Support · by Justin Young, 22 hours ago
1 40 21 hours ago
Support · by Duc , 1 week ago
2 76 6 days ago
Support · by Colin Hume, 1 week ago
2 70 6 days ago