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

醉酒当歌 提交于 2019-12-11 05:46:54

问题


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.


回答1:


You can use the bracket notation like this :

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


来源:https://stackoverflow.com/questions/39072618/can-a-liquid-for-loop-contain-a-page-variable-in-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!