I'm looking to create an html fragment in liquid/jekyll for the purposes of re-use across different areas. I'm able to pass data successfully from the parent html into the child fragment, but unable to figure out how to access passed variables within a forloop in the child.
parent.html:
{% assign section = 'work' %}
{% include child.html %}
child.html (fragment):
{{ section }} <!-- prints 'work' as expected -->
{% for scene in site.data.case-studies.[page.title].{{ section }}-scenes %}
<!-- this is not executed -->
<!-- note: also tried using bracket notation to access "section" in the above loop -->
{% for image in scene.TopLeft %}
<img id="top-left-{{ section }}" src="img/{{ section }}/top-left.svg" />
{% endfor %}
{% endfor %}
The forloop isn't executed because I'm not able to pass section = 'work'
to create a forloop such as: {% for scene in site.data.case-studies.[page.title].work-scenes %}
.
How can I pass the section variable into the forloop in liquid/jekyll?
You can either
{% assign section = 'work-scenes' %}
and then use
{% for scene in site.data.case-studies[page.title][section] %}
or, if you must not alter the scene
variable, create a new variable instead
{% assign sectionscenes = section | append: "-scenes" %}
and then use
{% for scene in site.data.case-studies[page.title][sectionscenes] %}
Ie, create a variable that holds the complete name of your property in the data. This also works across different files, because an {% include %}
more or less just replaces the statement with the content of the file.