Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jekyll can I pass a reference to a site data set in front matter

Tags:

jekyll

liquid

I have a layout used for the home page for different sections on my Jekyll site. On each of these pages I would like to have links to each item in the section, the details of which are stored in a YAML file in the site _data directory. My aim is to have the name of the site data variable in the section page front matter and pass this into the layout for rendering. For example:

Page Front Matter

---
sectionItems: site.data.sectionItems.awesomeSectionItems
---

...which is passed to the section home layout...

Section Home Layout

{% for item in page.sectionItems %}
    // Work with section item...
{% endfor %}

Unfortunately, when I run the site nothing appears. How would I go about doing this? I have also tried an include but this also does not work. I would like to avoid adding the for loop to each page, plus I would like the links to appear beneath the main content section.

like image 610
BWHazel Avatar asked Oct 15 '25 14:10

BWHazel


1 Answers

You cannot use variables in front matter. You will have to use a content variable like {% assign sectionItems = site.data.sectionItems.awesomeSectionItems %} and then loop with {% for item in sectionItems %}.

like image 129
David Jacquel Avatar answered Oct 19 '25 15:10

David Jacquel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!