matthewelsom
matthewelsom

Reputation: 944

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.

Upvotes: 4

Views: 1594

Answers (3)

speexy
speexy

Reputation: 114

Assuming your filenames are unique you could avoid a loop by filtering all pages for slug:

{% assign mypage = site.documents | where: "slug", "my-page-slug" | first %}
  {{mypage.title}}

If you have a page called "pageX.md" your slug would be "pageX".

Upvotes: 1

drocera
drocera

Reputation: 21

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.

Upvotes: 2

David Jacquel
David Jacquel

Reputation: 52829

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

Upvotes: 4

Related Questions