Garp
Garp

Reputation: 419

How to pass variables from a page to a layout in Liquid?

Suppose I have 3 categories called Food, Fruit and Milk, each containing specific posts.

The navbar shows 3 links to 3 pages that list all posts depending on the category type. For example, if I click on the link Food, it will redirect me to the page that shows only the articles of category Food.

I am thinking of creating a layout called category.html that shares among Food, Fruit and Milk. The only difference is the type of posts I want to pull out.

Is there any way to pass information from a page when calling a layout?

For example, in the page for Food category, I will call the category.html layout, but also pass a variable (e.g. type = 'Food'), and in the layout I will set up a condition check to pull only posts that match the variable type.

Can anyone give me some suggestion?

Upvotes: 4

Views: 3309

Answers (1)

rocambille
rocambille

Reputation: 15996

Declare your variable in the YAML frontmatter of your page, like this:

---
type: Food
---

You will then be able to access it in your layout through the properties of the page:

{{ page.type }}

Upvotes: 9

Related Questions