Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using liquid tags in YAML Front Matter variables

Tags:

Is it possible to use Liquid tags in YAML Front Matter variables?

For example if test.html contains:

---
variable: "Date: {% date: '%D' %}"
---
{{ page.variable }}

then Jekyll will generate the following HTML:

Date: {% date: '%D' %}

instead of something like:

Date: 03/13/14

Basically I'd like the Liquid tags in the YAML Front Matter variables to be processed.

like image 980
gvas Avatar asked Mar 13 '14 22:03

gvas


1 Answers

It sounds like you're trying to store a formatted date in a variable so you don't need to re-format the date each time you use it.

Rather than filtering the date in the front matter you could just add a Liquid capture statement just below the front matter. This will allow you assign your formatted date to a variable so you can use it in expressions.

---
title: Some sweet title
layout: default
date: 2014-9-17 # Could come from post's filename, but I put it here explicitly
---
{% capture formatted_date %}{{ page.date | date: "%-d %B %Y" }}{% endcapture %}

Once you have your new formatted date variable you can use it as an expression anywhere:

{{ formatted_date }} outputs: 17 September 2014

More on formatting the date itself.

like image 193
Roy Avatar answered Oct 28 '22 00:10

Roy