简体   繁体   English

将 Jekyll Liquid 变量(字符串)转换为 _data 文件路径

[英]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:我咨询了其他一些问题,但它们似乎不适用于这种情况:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM