For my Jekyll blog, I'm currently trying to add a feature that, for each page, processes data from an associated JSON file. I'm using the following structure (_data
directory with a subdirectory comments-gh
, which contains the JSON files):
./_data/
└── comments-gh
├── 14.json
├── 15.json
├── ...
├── ...
├── 76.json
└── 77.json
The number that makes up the base name of each file corresponds to a numerical identifier that is defined in the YAML frontmatter of each page by variable pagement_id
:
---
comment_id: 76
---
In theory this should allow me to access, for each page, the corresponding JSON, and then process it using something like this (or so I hope!):
{% assign comments = site.dataments-gh.pagement_id %}
{% for comment in comments %}
(processing code)
{% endfor %}
As it turns out, this doesn't actually work at all! But it does work if I hard-code the comment_id
value like this:
{% assign comments = site.dataments-gh.76 %}
So I suspect I'm doing something wrong with the way I'm using the pagement_id
variable. I couldn't find any clear examples in the Jekyll docs (which mostly cover the case of 1 single data file), but after some searching I did find this thread, which covers a similar issue:
So, I adapted my code along the lines of "Option #1" (the author of that post explicitly mentions omitting the dot and putting the variable between square brackets):
{% assign comments = site.dataments-gh[pagement_id].items %}
But that didn't solve the issue either! I'm probably overlooking something very basic (I don't work with Liquid often).
Does anyone have any suggestions?
(BTW I double-checked that pagement_id
is actually defined and has the expected value by writing its value to HTML output.)
For my Jekyll blog, I'm currently trying to add a feature that, for each page, processes data from an associated JSON file. I'm using the following structure (_data
directory with a subdirectory comments-gh
, which contains the JSON files):
./_data/
└── comments-gh
├── 14.json
├── 15.json
├── ...
├── ...
├── 76.json
└── 77.json
The number that makes up the base name of each file corresponds to a numerical identifier that is defined in the YAML frontmatter of each page by variable pagement_id
:
---
comment_id: 76
---
In theory this should allow me to access, for each page, the corresponding JSON, and then process it using something like this (or so I hope!):
{% assign comments = site.dataments-gh.pagement_id %}
{% for comment in comments %}
(processing code)
{% endfor %}
As it turns out, this doesn't actually work at all! But it does work if I hard-code the comment_id
value like this:
{% assign comments = site.dataments-gh.76 %}
So I suspect I'm doing something wrong with the way I'm using the pagement_id
variable. I couldn't find any clear examples in the Jekyll docs (which mostly cover the case of 1 single data file), but after some searching I did find this thread, which covers a similar issue:
https://talk.jekyllrb/t/page-variable-name-in-for-loop-using-data-files/7522/7
So, I adapted my code along the lines of "Option #1" (the author of that post explicitly mentions omitting the dot and putting the variable between square brackets):
{% assign comments = site.dataments-gh[pagement_id].items %}
But that didn't solve the issue either! I'm probably overlooking something very basic (I don't work with Liquid often).
Does anyone have any suggestions?
(BTW I double-checked that pagement_id
is actually defined and has the expected value by writing its value to HTML output.)
2 Answers
Reset to default 1I think the .items
part is wrong (it's not mentioned here). In your case, I think the syntax should be just:
{% assign comments = site.dataments-gh[pagement_id] %}
The combination of @andy-jackson's answer, and the comment by @mb21 about data types put me on the right track. It turns out that my problem was caused by the fact that the value of pagement_id
is an integer, whereas it needs to be a string to work in the file reference.
Surprisingly (at least to me), Liquid doesn't appear to have any built-in tags to convert an integer to a string! After some searching I did find this workaround that appends two quotation marks to an integer variable, which then magically returns a string. This works, but it's a bit hacky for my taste.
Digging into the docs on Liquid variables I did find the "capture" tag, which captures the string inside two tags, and then assigns the result to another (string) variable. I applied this to pagement_id
, and stored the result in a new variable comment_id
. I then used that variable in the way suggested by @andy-jackson.
Here's what I ended up with:
{% capture comment_id %}{{ pagement_id }}{% endcapture %}
{% assign comments = site.dataments-gh[comment_id] %}
{% for comment in comments %}
(processing code)
{% endfor %}
With these changes, the data file references work as expected on each page.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745076094a4609843.html
评论列表(0条)