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.
You can use page index in site.pages
. Eg : {{ site.pages[0].title }}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With