Can You Use Jekyll Page Variables in a Layout

Can you use Jekyll layout variables in pages?

After some research in Jekyll code, it seems that you cannot access layout variables from a page or post. The only information about layout a page can see is the layout's name {{ page.layout }}.

Then you can use this to store some layout related variables in _config.yml and get them from any post/page using the {{ page.layout }} variable.

_config.yml

layoutVars:
default:
relative_path: "../../"
post:
relative_path: "an other path"
page:
relative_path: "hello world"

use in a page or post

{% assign pagePath = site.layoutVars[page.layout].relative_path %}

Can you use Jekyll page variables in a layout?

I'm pretty sure this would work:

---
title: xxx
style: |
/* You can put any CSS here */
/* Just indent it with two spaces */
/* And don't forget the | after "style: " */
h2 {
color: red;
}
---

Your markdown/textile goes here. h2s will be red

And then in your layout:

<style type="text/css">
{{ page.style }}
</style>

And that should be it.

Jekyll Liquid Template: Use Variable of Layout in Page

You can put that variable outside the layout and posts, and then include it where you need it:

Create an include in _includes/customdata.html:

{% assign col = site.interfaceServer | where:"neededTopCollectionItem", page.topCollectionItem | sort: "order" %}

Then include it in someFile.md:

{%include customdata.html %}
{{col}}

or in the layout.

How can I pass a variable to a jekyll layout?

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>

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

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 }}

Jekyll: Page not parsing custom variable from layout

The flow of data is uni-directional.
default.html => hero.html => index.html

hero.html will not know what's defined in index.html

But, the rendering is in the opposite direction (inserted into the {{ content }} variable of the parent.

index.html ==> hero.html ==> default.html

Pass site collections from page to layout jekyll

Your flatify plugin is cool, but it does not reflect real life.

You cannot use liquid vars in front matter because they are not parsed.

In your page's front matter :

---
sec: "tt"
---

Then, from the page or the layout, you can just call :

{%- assign _sections = site[page.sec] -%}
{%- include header.html scrolly_nav=_sections -%}

If you want to debug, you can use inspect filter, which just outputs variable content.

{{ page.sec | inspect }} or {{ site[page.sec] | inspect }}


Related Topics



Leave a reply



Submit