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.

General

Struggling with blueprints

Solved by Alan Coggins View solution

Started by Alan Coggins 5 years ago · 3 replies · 585 views
5 years ago

I am having a little trouble getting my head around blueprints. I have made two new templates in my child theme: product and product_list. I have a new blueprint (product.yaml) that adds some extra fields like description and price. That's all good. But what I need to do is have another blueprint for product_list.yaml that adds the line below to the frontmatter so it knows to show a list of products:

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

If I add it manually it works fine, but I can't find a way to create this automatically when the product_list page is created. Am I wrong in thinking this can be done with blueprints?

5 years ago Solution

Got a reply on Discord. Thanks @Karmalakas. It's not necessary to use a blueprint, it can be done in the page template by using...

TWIG
{% for child in page.children() %}

Instead of {% for child in page.collection %}

👍 1
4 years ago

@ozfiddler, You have already picked an answer, but I wonder if that is what your want to achieve.

The answer you got on Discord is correct as a workaround for the problem you presented: Setting the collection using a blueprint.

However, the answer should depend on what you desire to achieve...

  • Hardcoded:
    Do you always only want to display the direct children of the "list" page, with same ordering and other params? Then the provided solution is fine.
  • Flexible:
    • Could it be that different list pages use different items in its collection? Eg. based on categories, descendants, ...
    • Could it be that different list pages use different collection properties, like ordering by price, or popularity instead of the default ordering by folder name?

If you want to provide flexibility you should go for frontmatter.

A collection can be defined in a blueprint. The following is added to the default.yaml blueprint of Quark:

YAML
collection:
  type: tab
  title: Collection Items

  fields:
    header.content.items:
      type: text
      label: Items in collection
      default: s[email protected]
    header.content.order.by:
      type: select
      label: Order by
      options:
        date: Date
        title: Title
        header.price: Price

Note: Above code should be indented so that 'collection' exactly aligns with 'advanced'.

Above definition will give the following page in Admin:
Untitled|690x328

The following frontmatter will be generated when 'Price' is selected:

YAML
---
title: Typography
content:
    items: s[email protected]
    order:
        by: header.price
---

And of course, you should now use {% for child in page.collection %} in the Twig template.

last edited 12/23/21 by pamtbaau
4 years ago

Great! Thanks for the detailed explanation. I'll give that a try. 👍

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 101 17 hours ago
General · by pamtbaau, 22 hours ago
1 68 21 hours ago
General · by Andy Miller, 1 day ago
0 53 1 day ago
General · by Marcel, 12 months ago
6 361 5 days ago
General · by Duc , 6 days ago
3 49 6 days ago