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

Loop through site.posts? #134

Closed
chrisfinazzo opened this issue Apr 2, 2019 · 3 comments
Closed

Loop through site.posts? #134

chrisfinazzo opened this issue Apr 2, 2019 · 3 comments
Labels

Comments

@chrisfinazzo
Copy link

For the examples listed on the layouts page, shouldn't the loop be {% for post in page.posts %}? As written, when I try this on my own site nothing renders, but if I use site.posts an archive appears.

Jekyll 3.8.5
Jekyll Archives 2.2.1
Ruby 2.5.0

@ashmaroli
Copy link
Member

From the source-code, it looks like page.posts is the correct accessor:

# posts - The array of posts that belong in this archive.
def initialize(site, title, type, posts)
@site = site
@posts = posts

Do you have a sample test-repository that I can clone and reproduce this..?

@chrisfinazzo
Copy link
Author

chrisfinazzo commented Apr 3, 2019

I don't have a testing repo, can reproduce my setup here. While this currently works, I'm wondering if there's a simpler way to go about it. For the record, page.posts is correct, my bad.

    jekyll-archives:
      enabled: year
      layout: archive
      permalinks:
        year: '/archive/:year/'
---
layout: default
---

{% for post in site.posts  %}
        {% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
        {% capture next_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %}

    {% if forloop.first %}
    <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
    <ul>
    {% endif %}

    <li><a href="{{ post.url }}">{{ post.title }}</a></li>

    {% if forloop.last %}
    </ul>
    {% else %}
        {% if this_year != next_year %}
        </ul>
        <h2 id="{{ next_year }}-ref">{{next_year}}</h2>
        <ul>
        {% endif %}
    {% endif %}
{% endfor %}

@DirtyF DirtyF added the support label Apr 3, 2019
@ashmaroli
Copy link
Member

What is your use-case @chrisfinazzo ?
The above Liquid code is quite a nasty hack! It considerably lowers you site's maintainability score.

I'm closing this since this does not look like a bug-report to me.
You may find assistance at Jekyll Talk..

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

3 participants