79483807

Date: 2025-03-04 13:20:23
Score: 0.5
Natty:
Report link

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 page.comment_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 though.

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 page.comment_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 %}{{ page.comment_id }}{% endcapture %}
{% assign comments = site.data.comments-gh[comment_id] %}

{% for comment in comments %}

    (processing code)

{% endfor %}

With these changes, the data file references work as expected on each page.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @andy-jackson's
  • User mentioned (0): @mb21
  • User mentioned (0): @andy-jackson
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: johan