Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Variables in Jekyll Front Matter

Tags:

jekyll

New to Jekyll and wondering if it's possible to include custom variables in Jekyll Front Matter. It would be useful for nested layouts, for example something like:

layouts/artist.html

----
layout: default
title: {{ page.artist }} (Artist)
----

I get an error trying that.

like image 663
mahemoff Avatar asked Apr 18 '12 22:04

mahemoff


People also ask

What is front matter in Jekyll?

Front matter is a snippet of YAML placed between two triple-dashed lines at the start of a file. You can use front matter to set variables for the page: --- my_number: 5 --- You can call front matter variables in Liquid using the page variable.

What is Frontmatter in Markdown?

When you create a Markdown file, you can include a set of key/value pairs that can be used to provide additional data relevant to specific pages in the GraphQL data layer. This data is called “frontmatter” and is denoted by the triple dashes at the start and end of the block.

How do I add YAML front matter?

YAML frontmatters can be defined at the beginning of a file, by starting on the first line with three dashes ( --- ) and ending the frontmatter either with three dashes or three dots (the former variant is more common). They contain valid YAML and can be used to define arbitrary variables.

What is liquid Jekyll?

What is Liquid? Liquid is a templating language used in Jekyll to process your site's pages. In other words, it helps you make your HTML pages a bit more dynamic, for example adding logic or using content from elsewhere. This doesn't require any setup - we can just start using it.


1 Answers

At the moment, Jekyll do not support Liquid variables in the front matter, and the only way to do that would be through a plugin, such as jekyll-conrefifier.


Alternatively, what you can do, though, is create variables that you re-use on the same file:

{% assign new_title = page.title | append: " (Artist)" %}
<h1>{{ new_title }}</h1>

and you can also pass variables to files that get included. For example, including a file from _includes\display-post.html passing the modified title as an argument:

{% assign new_title = page.title | append: " (Artist)" %}
{% include display-post.html post_title=new_title %}

And then getting the value of the value passed in (example content of _includes\display-post.html):

{% assign title_received = include.post_title %}

<h1>Title that as passed in: {{ title_received }}</h1>
like image 158
C. Augusto Proiete Avatar answered Sep 24 '22 11:09

C. Augusto Proiete