Nerde Nolzda

Accessing Jekyll Page Front Matter

As you might have noticed, I’ve added dates on pages like About, mainly because of schemas. However, even when date is added to the front matter, the following snippet in _layouts/page.html does not work at all:


<time datetime="{{ page.date | date_to_xmlschema }}" itemprop="datePublished">{{ page.date | date: "%b %-d, %Y" }}</time>

If the above is used, Jekyll shows the following error message:

Invalid Date: '' is not a valid datetime.
Liquid Exception: exit in _layouts/page.html

However, using {{ page.date }} directly displays the information as expected. After a lot of trial and error, I found a workaround:


{% capture pubDate %}{{ page.date }}{% endcapture %}
<time datetime="{{ pubDate | date_to_xmlschema }}" itemprop="datePublished">{{ pubdate | date: "%b %-d, %Y" }}</time>

The following also works:


<time datetime="{{ page['date'] | date_to_xmlschema }}" itemprop="datePublished">{{ page['date'] | date: "%b %-d, %Y" }}</time>

This might be related to how the following doesn’t work in Jekyll plugins:

Jekyll::Hooks.register :pages, :post_write do |document|
  print document.date
end

While the following works fine:

Jekyll::Hooks.register :pages, :post_write do |document|
  print document['date']
end

However, I do not know why this is the case. After all, this seems a bit unintuitive, and does not seem to be well-documented.

Related Posts

0 comments

Post a comment

Send an email to comment@nerde.pw.