How can I pass a variable to a jekyll layout?

Viewed 114

I don't understand how to pass a variable to a Jekyll layout. I have a markdown file with this header:

---
layout: post
next_tutorial: next_tutorial_name
---

And then, in the post layout, I have this:

---
layout: default
---
<article id="postBody">
    <h1>{{ page.title }}</h1>
    <p>{{ page.date | date_to_string }}</p>

    {{ content }}

    {% if site.next_tutorial %}
    <h2>{{ site.next_tutorial }}</h2>
    {% endif %}
</article>

But the h2 element never appears. I tried removing the if, but the result is the same. What I'm doing wrong?

1 Answers

What you are calling the "header of a markdown file" actually have a name in Jekyll, and this is called the Front Matter.

And the elements of the front matter can be accessed via the page variable, as pointed here and there.

So, your layout should read:

---
layout: default
---
<article id="postBody">
    <h1>{{ page.title }}</h1>
    <p>{{ page.date | date_to_string }}</p>

    {{ content }}

    {% if page.next_tutorial %}
    <h2>{{ page.next_tutorial }}</h2>
    {% endif %}
</article>
Related