stefano_cdn
stefano_cdn

Reputation: 1382

Jekyll - Can't Access Custom Front Matter Variables

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?

Upvotes: 5

Views: 1643

Answers (2)

stefano_cdn
stefano_cdn

Reputation: 1382

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.

Upvotes: 12

Ron
Ron

Reputation: 1161

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.

Upvotes: 1

Related Questions