Jekyll - 如何在单个include中包含多个参数?

3

是否可以在单个include中包含多个参数?

单个参数:

{% include card.html class=include.class1 %}

多个参数怎么办?

{% include card.html class=include.class1 class2=include.class2 %}

还是我需要这样做:class1=include.class1 class2=include.class2?

3个回答

6

可以通过在空格param1=value1 param2=value2之间传递多个include参数,例如:

{% include image.html url="http://jekyllrb.com"
max-width="200px" file="logo.png" alt="Jekyll logo"
caption="This is the Jekyll logo." %} 

然后你可以通过前缀为include.的方式在包含文件中使用它们,例如:

{{include.file}} {{include.caption}}

嘿,马克!谢谢。是的,我已经成功地解决了那个问题,更准确地说,我想将多个值传递到单个参数中。例如{% include card.html class=include.class1 && include.class2 %}。 - Rhys
@Rhys 你可以创建一个数组或将这些参数连接成一个字符串,然后将它们作为单个值传递,然后在 include 内部进行解构。不过我不明白为什么你需要这样做。 - marcanuy
没问题,谢谢。我唯一想要的情况是将多个类传递到组件中。例如,一个修饰符和禁用类。 - Rhys
再次感谢您的帮助,我能够使用捕获来实现这一点。@marcanuy - Rhys
@Rhys 很好!很高兴它有帮助! - marcanuy

0

正如@marcanuy所说,

一种方法是使用capture函数将多个值包含在单个参数中。

{% capture classes %} {{include.class1}} {{include.class2}} {% endcapture %}

{% include card.html class=classes %}


0
这是我的使用案例:
一个包含单选按钮集的HTML文件,例如:
<label>{{include.label}}</label> 
{% for option in include.options %}
<input type="radio" name="{{include.label}}" id="{{include.option}}" value="{{include.option}}" checked="checked"/><label for="  {{include.option}}">{{include.option}}</label>
{% endfor %}

你可以这样调用:

{% include radiobuttons.html label="favorite color" options="green", "blue", "orange", "red" %}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接