[英]Convert a Jekyll Liquid variable (string) to _data file path
I'm constructing a Jekyll site on Github pages.我正在 Github 页面上构建 Jekyll 站点。 I have a Github action that delivers fresh daily data to my _data/ file, with today's timestamp:我有一个 Github 操作,可以将最新的每日数据发送到我的 _data/ 文件,并带有今天的时间戳:
_data/
somedata_YYYYMMDD.json
I can construct a variable in a few ways to capture the filename for today's data file:我可以通过几种方式构造一个变量来捕获今天数据文件的文件名:
{% assign today_data = 'now' | date: "%Y%m%d" | prepend: 'site.data.somedata_' %}
or或者
{% capture today_data %}
site.data.somedata_{{ 'now' | date: "%Y%m%d" }}
{% endcapture %}
In both of these cases, if I try to put the today_data variable to use, Liquid interprets it as an inert string, rather than as a pointer to a liquid object .在这两种情况下,如果我尝试使用 today_data 变量,Liquid 会将其解释为惰性字符串,而不是指向液体对象的指针。 So, if I try {{today_data}}
, I get the string "site.data.somedata_20200902", but I'd like it to return the contents of the json file.因此,如果我尝试{{today_data}}
,我会得到字符串“site.data.somedata_20200902”,但我希望它返回 json 文件的内容。
I consulted a few other questions, but they don't seem to apply correctly to this situation:我咨询了其他一些问题,但它们似乎不适用于这种情况:
using Liquid variables inside of a liquid tag call - This person uses a variable as a string in a tag. 在液体标签调用中使用液体变量- 此人使用变量作为标签中的字符串。 I need the variable to be interpreted as a path to a data object.我需要将变量解释为数据对象的路径。
Using filters in Liquid tags - This person also creates a variable to be used as a string in a tag. 在 Liquid 标签中使用过滤器- 此人还创建了一个变量,用作标签中的字符串。
Use js variable with Shopify liquid tag - Same, I think: using a variable as a string in a tag. 使用带有 Shopify 液体标签的 js 变量- 我认为相同:使用变量作为标签中的字符串。
Use Liquid filter with Liquid Tag - This person is filtering the output of a tag. 使用带有液体标签的液体过滤器- 此人正在过滤标签的输出。
Almost there, that is a string in both those cases.几乎在那里,在这两种情况下都是一个字符串。 So it's effectively:所以它是有效的:
{{ 'site.data.somedata_YYYYMMDD' }}
Instead, you could do this to get access to and print the object:相反,您可以这样做来访问和打印对象:
{% assign today_path = 'now' | date: "%Y%m%d" | prepend: 'somedata_' %}
{{ site.data[today_path] | jsonify }}
This way you are accessing the site.data
object, and dynamically constructing the key/path.这样您就可以访问site.data
对象,并动态构建密钥/路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.