Reference front matter from a different page

Hi. I’m new to Jekyll.

I’m stuck with trying to reference front matter variables from another page in my templates. I’m wondering if it is possible to reference them directly without having to iterate over a for loop.

For example if I have an ‘about’ page and want the ‘title’ available from another page, can this be referenced in the other page with something along the lines of:

site.['about'].title

Hopefully my explanation makes sense :slight_smile: Thanks.

I don’t think that will work, what are you trying to accomplish? your example is a little too bare bones to come up with much of a solution.

You could build a data file or do it in the config file similar to how you have it. Just seems cumbersome to maintain.

Hi rdyar, thanks for the reply. Sorry, I was trying not to get too complicated in my question.

I was trying to create a way for a template to know what ‘section’ of a website a page is from. The Front matter might contain a variable like ‘section: about’, so that the template knows that the particular page is a child of the ‘about’ page. The template can then pull out some variable that it needs from the parent page (the actual use case is having a hero image for the top-level pages acting as a default for all the sub-pages in a section unless there is a hero image supplied for a particular sub-page).

I know you can get at the variables of any page by iterating over site.pages:

{% for item in site.pages %}
    find specific page and do something...
{% endfor %}

I was just wondering if there was a way to access another page and it’s front matter variables without the iterative process (which I assume slows build times down if you start to use lots of loops).

As it turns out, I have ended up using a data file and some front matter variables to achieve what I wanted, but I’d still be interested to know if that direct access is possible.

Hopefully that is a bit clearer.

Try this:

{% assign subpage = site.pages | where: 'section', 'about' %}

{% for item in subpage %}
   {{ item.title }}
{% endfor %}

There are many variations on this.

Thanks for that Bud. That’s pretty much the approach I took after discovering the where filter - very handy.

It’s interesting there isn’t a solution for this posted…

There’s a more elegant solution that does not require looping. Since the variable assignment results in an array and you’re trying to get the information from one singular page you can call the variable by it’s index:

{% assign subpage = site.pages | where: 'section', 'about' %}
{{ subpage[0].title }}

In the above example the array will output the single item so you’re calling the first item of the array with the [0]. I just had a similar scenario where I wanted to grab some front matter from my homepage and display it on another page and this worked!