Jekyll - Can't Access Custom Front Matter Variables
Asked Answered
P

2

5

I am new to Jekyll and would like to create additional variables in a Post Frontmatter:

style:
  name: post
img: image_name

When I try to use a variable like title it works

{% page.title %}

But when I try to use another variable

{% if page.img %}
  {{ page.img }}
{% else %}
  No image
{% endif %}

That returns nil. Even when simply trying to output

{{ page.img }}

Any idea why I can't use my custom variables defined in the frontmatter?

Pennebaker answered 15/4, 2016 at 17:29 Comment(3)
where are you putting the code? in a layout file? include? what is the whole front matter section? is there a repo to look at?Gladdy
I just pasted your front matter into a test post, and then your code block below and it output image_name as expected. Do you have the closing 3 dashes on the front matter? your code works, there is something wrong other than the code itself I think.Gladdy
Can you provide a repository url ? It's way more efficient for debugging.Carnage
P
12

After some research I discovered that my YAML FrontMatter variables were not read inside a layout file, and found this link:

https://github.com/jekyll/jekyll/issues/4123

So I changed

{{ page.img }}

to:

{{ layout.img }}

and now it works.

Pennebaker answered 17/4, 2016 at 19:14 Comment(0)
G
1

You should use {{ page.img }} instead of {% page.img %}. The percents in {% %} are usually for when you are doing something other than just calling a variable - like if statements, for statements, includes etc.

I wouldn't think {% page.title %} would work on its own.

Gladdy answered 15/4, 2016 at 19:3 Comment(1)
Thank you for your answer, unfortunately {{ page.img }} does not output anything.Pennebaker

© 2022 - 2024 — McMap. All rights reserved.