Using liquid tags in YAML Front Matter variables

时光总嘲笑我的痴心妄想 提交于 2019-12-23 07:19:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22392186/using-liquid-tags-in-yaml-front-matter-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!