在Jekyll中,我想循环列表中的项目,并为每个项目设置assign标记

在Jekyll中,我想循环列表中的项目,并为每个项目设置assign标记,jekyll,Jekyll,在Jekyll中,我想循环列表中的项目,并为每个项目分配变量 在数据文件中,我的列表如下所示: entries: - title: Sidebar subcategories: - title: Overview items: - title: Introduction url: /introduction/ linkname: intro - title: Release Notes

在Jekyll中,我想循环列表中的项目,并为每个项目分配变量

在数据文件中,我的列表如下所示:

entries:
- title: Sidebar
  subcategories:
    - title: Overview
      items:
        - title: Introduction
          url: /introduction/
          linkname: intro

        - title: Release Notes
          url: /release_notes/
          linkname: relnote
{% for entry in sidebar %}
  {% for subcategory in entry.subcategories %}
    {% for item in subcategory.items %}
      {% assign item.linkname = "<a href="{{ /item.permalink | prepend: site.baseurl}}">{{item.title}}</a>" %}
    {% endfor %}
  {% endfor %}
{% endfor %}
我的循环逻辑如下所示:

entries:
- title: Sidebar
  subcategories:
    - title: Overview
      items:
        - title: Introduction
          url: /introduction/
          linkname: intro

        - title: Release Notes
          url: /release_notes/
          linkname: relnote
{% for entry in sidebar %}
  {% for subcategory in entry.subcategories %}
    {% for item in subcategory.items %}
      {% assign item.linkname = "<a href="{{ /item.permalink | prepend: site.baseurl}}">{{item.title}}</a>" %}
    {% endfor %}
  {% endfor %}
{% endfor %}
然后我想在页面上使用{{intro}或{{relnote}}插入变量。使用此方法,我应该能够为侧边栏中的所有项目生成变量,从而可以轻松地在我的内容中嵌入链接


然而,这种方法似乎不起作用。插入变量时,该变量不会显示任何内容。我做错了什么?

使用Jekyll/Liquid,您无法从变量值创建变量名。

谢谢,David。顺便说一句,我发现你的测试用例对Jekyll很有帮助。但是,有一件事我不明白,那就是如何将项添加到数组中。我问了这个问题,但一直没有得到答案。我认为这个答案也可能有助于解决这个问题。你能指出我错在哪里吗?谢谢。我已经回答了杰基尔谈话。