getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.58k stars 1.41k forks source link

page.collection not returning any data #2576

Closed thejame closed 5 years ago

thejame commented 5 years ago

Try as I may, I cannot populate a collection with this page. Note that the page does not have any children (it's pulling the collection from another pages), and if I explicitly define the collection's content in the template it will bring back the data, but I need to fill the collection dynamically as I have several pages.

template (templates/podcast.html.twig):

{% set podcast_collection = page.collection %}
{{ podcast_collection.count }} <!-- this is zero -->
{% for podcast_item in podcast_collection %}
  {{ podcast_item.title }} <!-- this never shows -->
{% endfor %}
...

page (pages/podcasts/podcast-a/podcast.md):

---
title: Podcast A
content:
  items: '@page:/sermons'
  order:
    by: date
    dir: desc
  limit: 100
---

Thanks!

thejame commented 5 years ago

Using {% set podcast_collection = page.collection( {'items':{'@page':'/sermons'} ) %} gives me a collection to work with in the template, but I need to be able to send a different collection of items/pages to another podcast and would prefer to keep the template count low.

N-Parsons commented 5 years ago

You've got a subtle error in your page header. For your use case, items should contain a key-value pair, rather than a string. The twig snippet in your second comment actually shows you doing it this way!

...
content:
  items:
    '@page': '/sermons'
...

https://learn.getgrav.org/16/content/collections#page-or-page-children-collection-of-children-of-a-specific-page

thejame commented 5 years ago

Thanks, and I have tried that. It produces a completely blank page - no errors or anything on my rendered XML template output. What's even more weird is if I create a html.twig template I get site data and not page data, and if I create an xml.twig template I get a totally blank page.

I think it's the double : in the line - maybe it doesn't like having more than one unescaped colon?

N-Parsons commented 5 years ago

@thejame, there shouldn't be two colons in a line in the YAML front matter; the key-value pair goes on its own line (as in my code snippet above).

thejame commented 5 years ago

Thanks @N-Parsons, I understand the reasoning, but I am still not getting anything back using your proposed method. Either the list is blank or the page is blank. If you call items from another page in another folder, @page should work, right?

thejame commented 5 years ago

Update: I took some code that wasn't even referencing the page collection out of the template and now it renders with the Markdown file. Thank you for the heads-up with the key-value pair rules - that was super helpful to know.