John R Perry
John R Perry

Reputation: 4192

Can a liquid for loop contain a page variable in Jekyll?

Let's say I have a bunch of _data files that I use to create a list for specific pages. All of the pages that have these lists have a custom front matter variable of pageName. The value of pageName just so happens to match the _data file with the list.

Rather than pasting the html blocks of code into each page, I'd like to use an include to pull in a generic block of html. To do that, the include has to be dynamic or contain liquid markup to become dynamic.

That might be more context than you need, but my question boils down to this, is something like this possible:

{% for item in site.data.{{page.pageName}} %}
  {{ item.label }}
{% endfor %}

Unless I'm missing something, this doesn't work so I'm wanting to know if there's a way of achieving similar functionality.


I found this similar question, but it doesn't seem to answer whether it can be used to access a page variable to build a file name.

Upvotes: 3

Views: 1749

Answers (1)

David Jacquel
David Jacquel

Reputation: 52809

You can use the bracket notation like this :

{% for item in site.data[page.pageName] %}

Upvotes: 11

Related Questions