在 Liquid/Jekyll 中将变量传递给 Forloop

问题描述 投票:0回答:1

我希望在 Liquid/jekyll 中创建一个 html 片段,以便在不同领域重复使用。我能够成功地将数据从父 html 传递到子片段中,但无法弄清楚如何访问子片段中的 forloop 中传递的变量。

parent.html:

  {% assign section = 'work' %}
  {% include child.html %}

child.html(片段):

    {{ 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 %}

forloop 未执行,因为我无法通过

section = 'work'
来创建 forloop,例如:
{% for scene in site.data.case-studies.[page.title].work-scenes %}

如何将section变量传递到liquid/jekyll中的for循环中?

for-loop variables jekyll liquid
1个回答
0
投票

你可以

{% assign section = 'work-scenes' %}

然后使用

{% for scene in for scene in site.data.case-studies[page.title][section] %}

或者,如果您不能更改

scene
变量,请创建一个新变量

{% assign sectionscenes = section | append: "-scenes" %}

然后使用

{% for scene in for scene in site.data.case-studies[page.title][sectionscenes] %}

即,创建一个变量来保存数据中属性的完整名称。

© www.soinside.com 2019 - 2024. All rights reserved.