首页 文章

是否可以将参数传递给Jekyll中的集合成员

提问于
浏览
0

我有一个名为opcodes的集合,并创建了一个循环来生成所有成员的列表 . 缩写形式:

{% assign ops = site.opcodes | where: 'pst', 1 %}
{% for opcode in ops %}
<div id="op{{ opcode.n }}" class="opcode">
  <span class="op-data">#{{ opcode.n }} ({{ opcode.n | dec_to_hex }})</span>
  {{ opcode.content }}
{% endfor %}

个别成员是html文件,其中包含一些前沿问题 . 我将生成几个列表,成员有时需要稍微改变 . 看起来,当循环运行时,它们的内容已经生成了 . 我试图影响他们的前面的事情或网站变量,但这已经太晚了,没有一个流动的if语句工作 . 变量为空或处于原始值 .

Is there a way to make this work, somehow pass parameters to the members before their content gets rendered?

显而易见的解决方法是只复制有问题的文件并对其进行编辑,因为我已经使用“where”子句来选择它们 . 问题是关于任何更好的方法 .

编辑:这是各个文件的样子:

---
title: bla
pst = 0
---
bla bla
{% if x == "big" %}
  blu blu
{% else %}
  ble ble
{% endif %}

x 是我想从第一个循环中设置的外部 .

2 回答

  • 0

    您可以使用front matter default configuration根据包含的文件夹设置变量 .

    ─ _myCol
       ├── cat1
       │   ├── item0.md
       │   └── item1.md
       └── cat2
           ├── item2.md
           └── item3.md
    

    如果要为 myCol 集合中的所有项目设置变量 myVar ,您可以:

    # _config.yml
    defaults:
      - scope:
          type: myCol
        values:
          myVar: "YOLO"
    

    现在,如果要根据包含的文件夹为 myVar 设置不同的值,请添加:

    - scope:
        path: _myCol/cat1
      values:
        myVar: "TOTO"
    

    然后,您可以在集合的项目布局中使用此变量,如下所示:

    {% if page.myVar == "YOLO" %}
      blu blu
    {% else %}
      ble ble
    {% endif %}
    
  • 0

    我再次查看它,但没有找到解决方案,所以答案似乎是:不,在渲染之前无法将参数传递给集合成员 .

相关问题