在 YAML 变量中包含 jekyll/液体模板数据? [英] Include jekyll / liquid template data in a YAML variable?

查看:23
本文介绍了在 YAML 变量中包含 jekyll/液体模板数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Markdown 文件的 YAML 标题将 excerpt 变量添加到我可以在其他地方使用的博客文章中.在其中一个摘录中,我通过 markdown 链接标记引用了之前的一篇博客文章,并且我使用流动模板数据变量 {{ site.url }} 代替网站的基本 URL.

I am using the YAML heading of a markdown file to add an excerpt variable to blog posts that I can use elsewhere. In one of these excerpts I refer to an earlier blog post via markdown link markup, and I use the liquid template data variable {{ site.url }} in place of the base URL of the site.

所以我有类似的东西(稍微修剪一下)

So I have something like (trimmed it somewhat)

--- 
title: "Decluttering ordination plots in vegan part 2: orditorp()"
status: publish
layout: post
published: true
tags: 
- tag1
- tag2
excerpt: In the [earlier post in this series]({{ site.url }}/2013/01/12/
decluttering-ordination-plots-in-vegan-part-1-ordilabel/ "Decluttering ordination
plots in vegan part 1: ordilabel()") I looked at the `ordilabel()` function
----

但是,jekyll 和 Maruku md 解析器不喜欢这样,这让我怀疑你不能在 YAML 标头中使用液体标记.

However, jekyll and the Maruku md parser don't like this, which makes me suspect that you can't use liquid markup in the YAML header.

是否可以在 jekyll 处理的页面的 YAML 标头中使用液体标记?

Is it possible to use liquid markup in the YAML header of pages handled by jekyll?

  1. 如果是,我在显示的示例中做错了什么?
  2. 如果不允许允许,我还能用谁来实现我的意图?我目前正在我的笔记本电脑上开发我的网站,并且不想硬编码基本 URL,因为当我准备好部署时它必须更改.
  1. If it is, what I am I doing wrong in the example shown?
  2. If it is not allowed, who else can I achieve what I intended? I am currently developing my site on my laptop and don't want to hard code the base URL as it'll have to change when I am ready to deploy.

我从 Maruku 得到的错误是:

The errors I am getting from Maruku are:

| Maruku tells you:
+---------------------------------------------------------------------------
| Must quote title
| ---------------------------------------------------------------------------
|  the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-o
| --------------------------------------|-------------------------------------
|                                       +--- Byte 40

| Maruku tells you:
+---------------------------------------------------------------------------
| Unclosed link
| ---------------------------------------------------------------------------
| the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-or
| --------------------------------------|-------------------------------------
|                                       +--- Byte 41

| Maruku tells you:
+---------------------------------------------------------------------------
| No closing ): I will not create the link for ["earlier post in this series"]
| ---------------------------------------------------------------------------
| the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-or
| --------------------------------------|-------------------------------------
|                                       +--- Byte 41

推荐答案

我不相信在 YAML 中嵌套流动变量是可能的.至少,我还没有弄清楚该怎么做.

I don't believe it's possible to nest liquid variables inside YAML. At least, I haven't figure out how to do it.

一种可行的方法是使用 Liquid 的替换过滤器.具体来说,定义一个要用于变量替换的字符串(例如 !SITE_URL!).然后,在输出期间使用替换过滤器将其切换到所需的 Jekyll 变量(例如 site.url).这是一个精简的 .md 文件,它在我的 jekyll 0.11 安装中按预期运行:

One approach that will work is to use a Liquid's replace filter. Specifically, define a string that you want to use for the variable replacement (e.g. !SITE_URL!). Then, use the replace filter to switch that to your desired Jekyll variable (e.g. site.url) during the output. Here's a cut down .md file that behaves as expected on my jekyll 0.11 install:

---
layout: post

excerpt: In the [earlier post in this series](!SITE_URL!/2013/01/12/)

---

{{ page.excerpt | replace: '!SITE_URL!', site.url }}

在我的机器上测试,正确插入了 URL,然后按预期从 markdown 转换为 HTML 链接.如果要替换多个项目,可以将多个替换调用串在一起.

Testing that on my machine, the URL is inserted properly and then translated from markdown into an HTML link as expected. If you have more than one item to replace, you can string multiple replace calls together.

---
layout: post

my_name: Alan W. Smith
multi_replace_test: 'Name: !PAGE_MY_NAME! - Site: [!SITE_URL!](!SITE_URL!)'

---

{{ page.multi_replace_test | replace: '!SITE_URL!', site.url | replace: '!PAGE_MY_NAME!', page.my_name }}

重要的一点是,您必须明确设置 site.url 值.Jekyll 不是免费的.您可以在 _config.yml 文件中设置它:

An important note is that you must explicitly set the site.url value. You don't get that for free with Jekyll. You can either set it in your _config.yml file with:

url: http://alanwsmith.com

或者,在调用 jekyll 时定义它:

Or, define it when you call jekyll:

jekyll --url http://alanwsmith.com

这篇关于在 YAML 变量中包含 jekyll/液体模板数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆