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

Template Variables for Next/Previous posts within a category #260

Closed
surajram opened this Issue Dec 28, 2010 · 11 comments

Comments

Projects
None yet
8 participants
@surajram

surajram commented Dec 28, 2010

I separated different sections of a website using categories. But I couldnt use {{post.next}} and {{post.previous}} for chronological navigation between the posts as these kept linking to posts from all the categories.

I came up with the following code in my template to overcome it.

    {% for post in site.categories.photos %}
        {% if post.url == page.url %}
            {% assign post_index0 = forloop.index0 %}
            {% assign post_index1 = forloop.index %}
        {% endif %}
    {% endfor %}
    {% for post in site.categories.photos %}
        {% if post_index0 == forloop.index %}
            {% assign next_post = post.url %}
        {% endif %}
        {% if post_index1 == forloop.index0 %}
            {% assign prev_post = post.url %}
        {% endif %}
    {% endfor %}
    {% if next_post %}
        <a href="{{ next_post }}">Next Post</a> <br>
    {% endif %}
    {% if prev_post %}
        <a href="{{ prev_post }}">Previous Post</a> <br>
    {% endif %}

A simpler solution would be to have template variables like {{category.CATEGORYNAME.next}} and {{category.CATEGORYNAME.previous}}

@alexDarcy

This comment has been minimized.

Show comment
Hide comment
@alexDarcy

alexDarcy Nov 1, 2012

Thanks, I have been searching for that. For the time being, it will do.

alexDarcy commented Nov 1, 2012

Thanks, I have been searching for that. For the time being, it will do.

@hilliuse

This comment has been minimized.

Show comment
Hide comment
@hilliuse

hilliuse commented Jan 23, 2013

thanks

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Mar 17, 2013

Member

The best option would be to submit a patch to the pagination plugin. :)

Member

parkr commented Mar 17, 2013

The best option would be to submit a patch to the pagination plugin. :)

@parkr parkr closed this Mar 17, 2013

@SimplGy

This comment has been minimized.

Show comment
Hide comment
@SimplGy

SimplGy May 4, 2015

Is it safe to assume that the array in site.categories.photos is in time order?

SimplGy commented May 4, 2015

Is it safe to assume that the array in site.categories.photos is in time order?

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr May 4, 2015

Member

@SimplGy I can't say – try it out! Does it consistently hold true?

Member

parkr commented May 4, 2015

@SimplGy I can't say – try it out! Does it consistently hold true?

@SimplGy

This comment has been minimized.

Show comment
Hide comment
@SimplGy

SimplGy May 5, 2015

@parkr black box testing wouldn't really convince me (just because the ice cream consumption goes up when it's hot doesn't mean it always will). Trying to analyze it white box using the published source....

The jekyll team traverses them in a time dependent way here. I think it's good enough for my needs, but scanning the codebase for a few minutes I was unable to determine a firm "yes" or "no". There are no tests verifying the time order of the posts in categories.

I don't know ruby well--is the order of the keys in a ruby hash deterministic? In JS it is not, but most implementations behave predictably. If the same is true of Ruby it may look like posts are in time order most of the time but not be guaranteed.

SimplGy commented May 5, 2015

@parkr black box testing wouldn't really convince me (just because the ice cream consumption goes up when it's hot doesn't mean it always will). Trying to analyze it white box using the published source....

The jekyll team traverses them in a time dependent way here. I think it's good enough for my needs, but scanning the codebase for a few minutes I was unable to determine a firm "yes" or "no". There are no tests verifying the time order of the posts in categories.

I don't know ruby well--is the order of the keys in a ruby hash deterministic? In JS it is not, but most implementations behave predictably. If the same is true of Ruby it may look like posts are in time order most of the time but not be guaranteed.

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr May 5, 2015

Member

In my experience, the order of keys in a ruby hash is deterministic, yes.

Member

parkr commented May 5, 2015

In my experience, the order of keys in a ruby hash is deterministic, yes.

@envygeeks

This comment has been minimized.

Show comment
Hide comment
@envygeeks

envygeeks May 5, 2015

Contributor

In Ruby the spec is insertion unless you enumerate it.

Contributor

envygeeks commented May 5, 2015

In Ruby the spec is insertion unless you enumerate it.

@envygeeks

This comment has been minimized.

Show comment
Hide comment
@envygeeks

envygeeks May 5, 2015

Contributor

To Add: Of which that specific piece of Liquid code does do, it enumerates the array/hash.

Contributor

envygeeks commented May 5, 2015

To Add: Of which that specific piece of Liquid code does do, it enumerates the array/hash.

@envygeeks

This comment has been minimized.

Show comment
Hide comment
@envygeeks

envygeeks May 5, 2015

Contributor

And by enumerate I mean enumerate over it in an order you wish to.

Contributor

envygeeks commented May 5, 2015

And by enumerate I mean enumerate over it in an order you wish to.

@dotnil

This comment has been minimized.

Show comment
Hide comment
@dotnil

dotnil Jan 14, 2016

{% if page.categories %}
  {% assign category = page.categories[0] %}
  {% assign posts = site.categories[category] %}
  {% for post in posts %}
    {% if post.url == page.url %}
      {% assign post_index0 = forloop.index0 %}
      {% assign post_index1 = forloop.index %}
    {% endif %}
  {% endfor %}
  {% for post in posts %}
    {% if post_index0 == forloop.index %}
      {% assign next_post = post %}
    {% endif %}
    {% if post_index1 == forloop.index0 %}
      {% assign prev_post = post %}
    {% endif %}
  {% endfor %}
{% endif %}

Modified a bit to allow arbitrary categories. Also changed the type of prev_post and next_post to Post rather than just the url.

dotnil commented Jan 14, 2016

{% if page.categories %}
  {% assign category = page.categories[0] %}
  {% assign posts = site.categories[category] %}
  {% for post in posts %}
    {% if post.url == page.url %}
      {% assign post_index0 = forloop.index0 %}
      {% assign post_index1 = forloop.index %}
    {% endif %}
  {% endfor %}
  {% for post in posts %}
    {% if post_index0 == forloop.index %}
      {% assign next_post = post %}
    {% endif %}
    {% if post_index1 == forloop.index0 %}
      {% assign prev_post = post %}
    {% endif %}
  {% endfor %}
{% endif %}

Modified a bit to allow arbitrary categories. Also changed the type of prev_post and next_post to Post rather than just the url.

itspladd added a commit to itspladd/itspladd.github.io that referenced this issue Feb 6, 2016

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.