Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with nested collections #1097

Closed
Sogl opened this issue Oct 8, 2016 · 12 comments
Closed

Problem with nested collections #1097

Sogl opened this issue Oct 8, 2016 · 12 comments
Labels

Comments

@Sogl
Copy link
Contributor

Sogl commented Oct 8, 2016

Hello!

I'm trying to implement Wordpress style archives in my Grav site.
For example,
Post: https://bestblog.wordpress.com/2007/06/13/elle-effect/
Day: https://bestblog.wordpress.com/2007/06/13/
Month: https://bestblog.wordpress.com/2007/06/
Year: https://bestblog.wordpress.com/2007/

I decided to use nested folders to work with it, but found some problems. First, I'm trying to work with adding year folders.

1_ Problem with pagination.

I use nested loop:

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

<!-- Year loop -->
{% for postYear in yearCollection %}

<!-- Post collection -->
{% set postCollection = postYear.collection() %}

<!-- Post loop -->
{% for post in postCollection %}

....my html output...

{% endfor %}

{% endfor %}

{{ dump(postCollection.params.pagination) }}

{% if config.plugins.pagination.enabled and postCollection.params.pagination %}
    {% include 'partials/pagination.html.twig' with {'base_url':page.url, 'pagination':postCollection.params.pagination} %}
{% endif %}

And my dump is always null, I can get yearCollection pagination parameter only, not postCollection that I need.

Can I get all nested items in one BIG collection and set pagination with it?

2_ Before year directories I used this construction to output events on main page:

process:
    twig: true
    markdown: false
events:
    items:
        '@page': '/events'
    order:
        by: date
        dir: desc
    dateRange:
        start: today

Now with this code I can get only years, but can't get posts directly with '@page': '/events-year', also it doesn't work with '@page': '/2016. Why?

3_ Third problem is copy&pasting the same files for post in every year directory:
image
Without them I get error 404.

Is it possible to get rid all of these problems?

@Perlkonig
Copy link
Contributor

There are plugins to generate these sorts of archives. Have you looked at how they implement this? I know this doesn't answer your questions directly. I'd just hate for you to waste time if there's a plugin that will get where you want to go.

@Sogl
Copy link
Contributor Author

Sogl commented Oct 8, 2016

@Perlkonig This issue was opened long time ago in grav-plugin-archives repo:
getgrav/grav-plugin-archives#16
I don't know any other type of plugin that can do this job for me...

I did a more deep research and found a way how to solve my 2nd problem.
In YAML:

'@page.descendants': '/events'

In md:

{% set events_collection = page.collection('events').ofType('post') %}

Desc do a recurse through folders and this good filter do a final job 👍

But it did't work for pagination (1st problem) 😞
I found an issue where this problem is discussed:
getgrav/grav-plugin-pagination#18
Will be some movement on this issue?

3rd problem is still open...

@flaviocopes
Copy link
Contributor

postCollection in your example is null because of the scope, as it's defined inside the for but you access it outside of it. Use http://twig.sensiolabs.org/doc/filters/merge.html to populate an array in the iteration.

@Sogl
Copy link
Contributor Author

Sogl commented Oct 11, 2016

@flaviocopes

Use http://twig.sensiolabs.org/doc/filters/merge.html to populate an array in the iteration.

Can I use merge to put collections into each other?

And what about my other questions?

@flaviocopes
Copy link
Contributor

If you define postCollection inside the for, it's not accessible outside it.

If you access /2016 as a page, then it must have a markdown file in it, otherwise you'll get a 404.

@Sogl
Copy link
Contributor Author

Sogl commented Oct 11, 2016

I solved problems with scope and need help with 1st and 3rd problems.

@Sogl
Copy link
Contributor Author

Sogl commented Oct 11, 2016

@rhukster Why this issue was closed without any comment?

@rhukster
Copy link
Member

oh sorry, i just read "I solved problems..." and thought you were all sorted. reopening.

@rhukster rhukster reopened this Oct 11, 2016
@Sogl
Copy link
Contributor Author

Sogl commented Oct 11, 2016

@rhukster Don't worry 😉
I would be very happy if you can help me with 1st and 3rd questions. Please read my comments here:
#1097 (comment)
#1097 (comment)

@Sogl
Copy link
Contributor Author

Sogl commented Oct 16, 2016

Why I can't use only one frontmatter.yaml with template variable in it?

Documentation:
https://learn.getgrav.org/content/headers#template

How it looks:
image

frontmatter.yaml content:

template: events
content:
    items: @self.children
    order:
        by: date
        dir: desc

Error 404.
Why?

UPDATE

Finally I can reduce number of files with following:
image

Every events.md contains:

---
content:
    items: @self.descendants
    order:
        by: date
        dir: desc
---

Multilang lines I put into languages.yaml.
And part from events.html.twig:

{% extends 'partials/base.html.twig' %}

{% set level1 = page.title %}
{% set level2 = page.parent.title %}
{% set level3 = page.parent.parent.title %}

{% if level1|length == 2 and level2|length == 2 and level3|length == 4 %}
    {% set myTitle = 'MONTHS_OF_THE_YEAR'|ta(level2|abs - 1) ~ " " ~ level1 ~ ", " ~ level3 %}
    {% set mySubheader = t('HEADER.SUBHEADER_DAY') %}
{% elseif level1|length == 2 and level2|length == 4 %}
    {% set myTitle = 'MONTHS_OF_THE_YEAR'|ta(level1|abs - 1) ~ " " ~ level2 %}
    {% set mySubheader = t('HEADER.SUBHEADER_MONTH') %}
{% elseif level1|length == 4 %}
    {% set myTitle = level1 %}
    {% set mySubheader = t('HEADER.SUBHEADER_YEAR') %}
{% endif %}

{% block title %}{{ myTitle }} &mdash; {% endblock %}{% block content %}

{% set places = page.find('/places').header.places %}
{% set collection = page.collection().ofType('post') %}

...

<h1 class="brand-heading">{{ myTitle }}</h1>
<p class="intro-text">{{ mySubheader }}</p>
...

@rhukster Can you add an option to use the same template in nested folders? This option removes the creation of files in each folder.
For example:

---
template: my-template
template_nested: events
---

@rhukster
Copy link
Member

Closing to due to inactivity

@Sogl
Copy link
Contributor Author

Sogl commented Sep 13, 2017

@rhukster Because no answers from developers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants