1

In a Jekyll layout, default.html, you initialize a variable

{% assign relative_path = "../../" %}

Inside a page using default layout, this variable is empty though. How can you use inside pages variables set in layout?

This is the opposite of Can you use Jekyll page variables in a layout?

Community
  • 1
  • 1
Marius
  • 1,659
  • 3
  • 22
  • 31

2 Answers2

5

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 %}
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
1

You have two possibilities.

  1. Store your variable in the layouts yaml-header, e.g.:
    ---
    variable : value
    ---

  2. Have a look at jekylls front matter defaults. You can declare variables depending on the type of posts/documents and the folder the posts are stored in.

I'm not if this is what you are looking for, but i hope it helps...

astark
  • 609
  • 4
  • 7