英文:
Jekyll get Attributes of Collection Element instead of Output
问题
我有一组元素,它们的输出为 true。现在我想创建一个数组,其中只包含这些集合元素的属性,而不包括输出。然而,如果我使用 {{ assign arr = arr | push: site.col['elem'] }},它不起作用,因为这会给我输出。
另外,我尝试了 {{ assign arr = arr | push: [site.col['elem'].attr1, site.col['elem'].attr2] }},但这给我了以下 Liquid 错误:
Liquid 异常:Liquid 错误(第86行):参数数量不正确(给定3个,期望2个)在(路径)
有没有办法实现我需要的效果?
英文:
I have a collection of elements that have output on true. Now I'd like to make an array of some of those Collection elements, but only its attributes and not the output. However, if I use {{ assign arr = arr | push: site.col['elem'] }} it doesn't work, as this gives me the output.
Alternatively, I tried {{ assign arr = arr | push: [site.col['elem'].attr1, site.col['elem'].attr1] }} but this gave me the following Liquid Error:
> Liquid Exception: Liquid error (line 86): wrong number of arguments (given 3, expected 2) in (PATH)
Is there any way to achieve what I need?
答案1
得分: 0
Push仅接受2个参数。您可以使用for循环并将所需的属性添加到哈希表中。
代码检查集合的输出属性是否为true,并将所需的属性提取到名为elem_attributes的新对象中,然后将其推送到结果数组中:
{% assign arr = [] %}
{% for elem in site.col %}
{% if elem.output == true %}
{% assign elem_attributes = { 'attr1': elem.attr1 } %}
{% assign arr = arr | push: elem_attributes %}
{% endif %}
{% endfor %}
我希望我理解得正确。
英文:
Push only accepts 2 arguments. You can use a for loop and add desired attributes to a hash.
The code checks if the output attribute of a collection is true and extracts the desired attributes into a new object called elem_attributes before pushing it to the result array:
{% assign arr = [] %}
{% for elem in site.col %}
{% if elem.output == true %}
{% assign elem_attributes = { 'attr1': elem.attr1 } %}
{% assign arr = arr | push: elem_attributes %}
{% endif %}
{% endfor %}
I hope I got you right.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论