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.

Forms & Blueprints

Creating Collections and Listing Pages

Started by Brian Callies 9 years ago · 2 replies · 1733 views
9 years ago

Greetings,

I'm a novice and a dabbler when it comes to web development, so please bear with me. I'm working on a personal project that will, among other things, contain a database of characters in a game I'm running.

I have a character.yaml blueprint and its corresponding character.html.twig template which are both working fine (though the template style and layout is far from finished).

That's great and I enjoyed figuring out how to do those, but at the moment I'm struggling a bit with Collections.

My goal is to have a page with a grid, or list, of all the characters which then can be clicked on to open a particular characters page.

From my understanding, I would create a blueprint called, for example, characters.yaml with something like:

YAML
title: Star Wars Characters

content:
  items: '@self.children'
  order:
    by: date
    dir: desc
  limit: 10
  pagination: true

Then I would create a Characters folder under which I would create each individual character?

Then I need to create a characters.html.twig template that would cycle through each character page and display a portrait, name, or whatever else?

If all of that is correct, then I'm doing something wrong, possibly at multiple levels. I think it may be my characters.html.twig file. Should the following work?:

TWIG
{% embed 'partials/base.html.twig' %}

    {% set collection = page.collection() %}

    {% block content %}
           <div>
            {% for child in collection %}
                   {{ page.header.character.name }}
                {% endfor %}
         </div>
    {% endblock %}

{% endembed %}

Maybe I should phrase my question better: Could someone explain, perhaps in simpler terms than the manual, how to create a collection of pages and display a list of those pages?

Thank you,
Brian

9 years ago

Okay, I see this is definitely wrong:

TWIG
            {% for child in collection %}
                   {{ page.header.character.name }}
                {% endfor %}

Should the following work:

TWIG
            {% for p in page.collection %}
                   {{ p.header.character.name }}
                {% endfor %}
9 years ago

It looks like I got it working. The listing page can now "access" the pages in the collection and display names and other data.

Besides the mistake in the twig file above, I also needed to manually edit the characters.md file with the collection information:

YAML
title: 'Characters List'
creator: brian
sitemap:
    changefreq: monthly
content:
    items: '@self.children'
    limit: 5
    order:
        by: date
        dir: desc
    pagination: true
    url_taxonomy_filters: true

I thought that stuff would be picked up from the blueprint, but maybe I did something wrong.

Thanks for letting me clutter up the forums while I worked this out.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1138 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 135 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 110 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 129 7 months ago