Overriding defaults in layout frontmatter does not affect page


#1

If I have a frontmatter on a layout that sets a variable, does that variable apply on the page ?

I have a page with frontmatter and content

---
layout: test
---
{{ myvalue }}

and I have a layout called test with frontmatter

---
myvalue: foo
---

The page does not display foo.

Having read this tutorial I think this doesn’t work because the layout is processed after the page, although that is talking about variables assigned in a layout’s liquid rather than its front matter.

My use case is to override defaults when a specific layout is used. So I have defaults defined in _config.yml but want to change some of those when a specific layout is used. Perhaps there is another way…?


#2

Page variables in front matter are accessed via the page object while layout front matter is layout.

What you probably want to do is use assign to set a new variable that defaults to layout.myvalue but can be overided at the page level.

Something like this:

{% assign myvalue = page.myvalue | default: layout.myvalue %}

And you’d access it with {{ myvalue }} which should output foo. If you set myvalue: bar in a page’s front matter instead of the layout it should output bar.


#3

Hi @mmistakes, what I am trying to do is set the theme’s toc setting in a layout instead of setting it in the page.

I have toc: true set as a default in _config.yml but I want to disable it for my index pages. I have a _layouts/index.md file where I set toc: false in the frontmatter hoping that pages using that layout would not get a toc. But it doesn’t work like that.

If I have to set something in the page file then I might as well do toc: false in its frontmatter. It’s what I am doing now but I hoped that I could factor that out to the layout.

Sorry - I posed the question in a generic way rather than calling out the theme… :slight_smile:


#4

Ah yeah that’s the problem. The theme looks for page.toc not layout.toc.

Your best bet would be to set a front matter default and scope it to the pages you want that value for.