Skip to content

Unexpected behavior when using reversed and limit on for..in loop #312

Closed
@pdehaan

Description

@pdehaan

Found in [email protected] (via @11ty/eleventy@canary).

I noticed that when using a for..in loop with the reversed and limit attributes, it always seems to apply limit first (so I only get items "one" and "two", regardless of which order I define reversed and limit:2):

{% assign arr = "one,two,three,four,five" | split: "," %}

{% for item in arr reversed limit: 2 %}
- {{ item }}
{% endfor %}

{% comment %}
OUTPUT
- two
- one
{% endcomment %}

In this case, I expected reversed to get applied first so limit:2 would pluck items "five", and "four", respectively.


Workaround seems to be to define "reverse" filter in the {% assign %} tag instead of in the for..in loop:

{% assign arr = "one,two,three,four,five" | split: "," | reverse %}

{% for item in arr limit: 2 %}
- {{ item }}
{% endfor %}

{% comment %}
OUTPUT
- five
- four
{% endcomment %}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions