如何在液体中串联数组

时间:2019-02-10 08:57:29

标签: jekyll liquid

这种问与答曾经存在过,但我觉得很合适。 我想在空数组和数组内将液体中的数组连接起来

array1
subject = ''
array2
tsubject = ["appple", "pine appeld"]
排列我想要得到的东西
["appple", "pine appeld"]

结合我的尝试

{% assign subject = '' %}
{% for post in site.programming %}
  {% assign tsubjects = post.categories %}
  {% assign subject=subject | append: tsubjects %}
{% endfor %}
{% assign subject = '' %}
{% for post in site.programming %}
  {% assign tsubjects = post.categories %}
  {% subject=subject | concat: tsubjects %} 
{% endfor %}

但没有任何变化,主题仍然为空。 我认为这是因为concatconcat会像这样连接数组形状:
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}
但我想在下面的类型中串联:
{% assign vegetables = ["broccoli", "carrots", "lettuce", "tomatoes"] | split: ", " %}
我不确定它们为什么不起作用。请帮助我。

1 个答案:

答案 0 :(得分:0)

您正在混合字符串/数组值和过滤器。

{% assign subject = '' %}
subject : {{ subject | inspect }} "" <== String

{% assign subject = '' | split: '' %}
subject : {{ subject | inspect }} [] <== Array

如果要从编程集合中的所有类别中组成一个数组,则必须concat两个数组,它可能类似于:

{% comment %} --- Creates an empty array {% endcomment %}
{% assign subject = '' | split: '' %}

{% comment %} --- Debug output {% endcomment %}
subject : {{ subject | inspect }}

{% for post in site.programming %}
  {% assign tsubjects = post.categories %}

  {% comment %} --- Just to be sure that there is something in post.categories {% endcomment %}
  tsubjects : {{ tsubjects | inspect }}

  {% assign subject=subject | concat: tsubjects %}

  {% comment %} --- Debug output {% endcomment %}
  concat : {{ subject | inspect }}

{% endfor %}

{% comment %} --- Makes sure that values are unique {% endcomment %}
{% assign subject = subject | uniq %}
uniq : {{ subject | inspect }}