Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a specific page variable in Jekyll

Is it possible to call a specific page.variable into a layout in Jekyll?

I know it is possible to use {{ page.title }} in a layout to call the current page title, but is it possible to call another page's title instead.

e.g.

{{ pageX.title }} 

Would return the title from pageX.html

I have searched and couldn't see a solution, I just wanted to see if anyone knows if this is even possible.

like image 994
matthewelsom Avatar asked Mar 16 '23 02:03

matthewelsom


2 Answers

You can use page index in site.pages. Eg : {{ site.pages[0].title }}

like image 173
David Jacquel Avatar answered Apr 25 '23 20:04

David Jacquel


It depends on how do you want to select the particular page. For example, I have a page category called "units" (accessible under "site.categories.units"). For every "unit" page I defined the YAML varibable "unit-id" in a unique (and human readable) way.

Imagine that I want to access the unit with the "unit-id" "my-first-unit". I can access the content of this page using:

{{ site.categories.units | where: "unit-id","my-first-unit" }}

If instead I want to do more complex things and I need to access any of the information stored in the YAML preamble (let's call it "whatever-YAML-variable") of this particular page, it's a bit more involved:

{% assign this-unit = "my-first-unit" %}
{% for unit in site.categories.units %}{% if unit.unit-id == this-unit %}{{ unit.whatever-YAML-variable }}{% break %}{% endif %}{% endfor %}

Notice that the instructions inside de loop are contracted (without linebreaks) to avoid the appearance of numerous empty lines in the output HTML. Furthermore, I added a "break" instruction to cut the waste of processor resources.

Also notice that you can easily add a few lines to control if the "my-first-unit" page exists, or even solve conflict in case of duplicates.

I admit that this is a rather clumsy way to do it, but it's a first solution. I will be looking forward to any other more robust suggestion.

like image 37
drocera Avatar answered Apr 25 '23 18:04

drocera