How to use a variable as array index in Jekyll include

Viewed 2383

I have a data file containing objects that I want to use specified indexes in a Jekyll include.

My page is calling the include like so:

{% include components/header-filter.html items="0" %}

Where 0 is the index I want to use in this case.

My include is:

{% assign filterID = include.items %}
{% capture id %}{{ filterID }}{% endcapture %}
{% assign filter = site.data.filters[id] %}

This works until it gets to the 3rd line, at which point it outputs nothing.

What am I doing wrong?

Cheers!

3 Answers

Use the forloop helper

  • forloop.length -- length of the entire for loop
  • forloop.index -- index of the current iteration
  • forloop.index0 -- index of the current iteration (zero based)
  • forloop.rindex -- how many items are still left?
  • forloop.rindex0 -- how many items are still left? (zero based)
  • forloop.first -- is this the first iteration?
  • forloop.last -- is this the last iteration?
{% for offer in site.data.companies %}
  <li data-target="#carouselExampleCaptions" data-slide-to="{{forloop.index0}}"></li>
{% endfor %}

This will give

<li data-target="#carouselExampleCaptions" data-slide-to="0"></li>

<li data-target="#carouselExampleCaptions" data-slide-to="1"></li>

<li data-target="#carouselExampleCaptions" data-slide-to="2"></li>

...
Related