SexxLuthor
SexxLuthor

Reputation: 4556

Jekyll front matter variable not being set in intermediate layout?

In my default layout, I'm referencing a variable {{page.headline}}, and am referencing it as follows:

# _layouts/default.html
{{page.headline}}

# index.md
---
layout: default
headline: Great site
---

# _layouts/post.html
---
layout: default
headline: Great articles
---

# _posts/some-post.md
---
layout: post
headline: Great post # just for testing, and omitted when setting in post layout
---

The headline variable at the index--at Level 1--works as expected, and also at Level 3, in a specific post. But when I set "Great articles" in the post layout (and remove the variable setting from the front matter of some-post.md), nothing renders in the default layout's variable placeholder.

In other words, in the post layout, I don't see "Great articles" or "Great site," so it's like the initial variable setting of "Great site" in the default layout--which the post layout references--is being overwritten, but not replaced.

Yet this works at the third level with some-post.md--setting headline here, where the specific post uses the post layout, and the post layout itself uses default, does cause page.headline to be set to "Great post."

Should this work in the way I'm hoping for here? To set a variable in the front matter of a second layout that is based upon a first? (And again it's weird that it works at the bottom level, with the post itself.) Thanks.

(In reality I don't want to set it at the third level on each post, since it's just a generic "Articles" header--I'm including that here because of it working at that level, as explained above.)

Upvotes: 3

Views: 351

Answers (1)

SexxLuthor
SexxLuthor

Reputation: 4556

The solution is to reference the variable via the layout, and not the page, as described here.

So, reference the variable in the layout as {{ layout.headline }} instead of {{ page.headline }}, and this can now be set via the front matter of an inheriting template (i.e., a template that itself specifies a parent template in its own front matter: layout: default).

Upvotes: 4

Related Questions