Tags Uniq液体过滤器标签赢得';在字符串中循环单词时不起作用

Tags Uniq液体过滤器标签赢得';在字符串中循环单词时不起作用,tags,liquid,uniq,Tags,Liquid,Uniq,我想列出我帖子中的所有标签,当然每个标签中只有一个,所以一个标签中没有几个 我试着把它放在一个字符串中,用空格分隔它们,然后循环字符串中的每个单词,给它们uniq过滤器: {% capture alltags %} {% for story in site.stories %} {{ story.tags | join: ' ' }} {% endfor %} {% endcapture %} {% for word in alltags %} {{ word | uniq }}

我想列出我帖子中的所有标签,当然每个标签中只有一个,所以一个标签中没有几个

我试着把它放在一个字符串中,用空格分隔它们,然后循环字符串中的每个单词,给它们uniq过滤器:

{% capture alltags %}

{% for story in site.stories %}

{{ story.tags | join: ' ' }}

{% endfor %}

{% endcapture %}

{% for word in alltags %}

{{ word | uniq }}

{% endfor %}
我得到了单词之间的空格,但它们不是uniq。
我确实需要将它们单独循环,这样我就可以在它们上建立链接。

类似的东西可以工作

{% comment %} compiling the gross list of all tags, duplicates and all {% endcomment %}
{% for post in site.posts %}
  {% assign tags = tags | concat:post.tags %}
{% endfor %}
{% comment %} Getting rid of duplicates (uniq), sorting it  - all in one go {% endcomment %}
{{ tags | uniq | sort }}

像这样的东西会有用的

{% comment %} compiling the gross list of all tags, duplicates and all {% endcomment %}
{% for post in site.posts %}
  {% assign tags = tags | concat:post.tags %}
{% endfor %}
{% comment %} Getting rid of duplicates (uniq), sorting it  - all in one go {% endcomment %}
{{ tags | uniq | sort }}

如果你尝试一下,你就会明白发生了什么

{% capture alltags %}
{% for story in site.stories %}
{{ story.tags | join: ' ' }}
{% endfor %}
{% endcapture %}

alltags : {{ alltags | inspect }}

{% for word in alltags %}
word : {{ word | inspect }}

uniq : {{ word | uniq }}
{% endfor %}
alltags
是字符串,而不是数组

alltags
上循环时,发生的唯一循环包含
word
变量,该变量是一个等于
alltags
本身的字符串

实际上,您需要在数组上应用
uniq
过滤器

如果您运行此代码,您将看到差异:

{% comment %} create an empty array {% endcomment %}
{% assign tagsArray = "" | split:"" %}

{% for story in site.stories %}
  {% assign tagsArray = tagsArray | concat: story.tags %}
  tagsArray : {{ tagsArray | inspect }}
{% endfor %}

tagsArray : {{ tagsArray | inspect }}

{% assign tagsArray = tagsArray | uniq %}

tagsArray uniq : {{ tagsArray | inspect }}

如果你尝试一下,你就会明白发生了什么

{% capture alltags %}
{% for story in site.stories %}
{{ story.tags | join: ' ' }}
{% endfor %}
{% endcapture %}

alltags : {{ alltags | inspect }}

{% for word in alltags %}
word : {{ word | inspect }}

uniq : {{ word | uniq }}
{% endfor %}
alltags
是字符串,而不是数组

alltags
上循环时,发生的唯一循环包含
word
变量,该变量是一个等于
alltags
本身的字符串

实际上,您需要在数组上应用
uniq
过滤器

如果您运行此代码,您将看到差异:

{% comment %} create an empty array {% endcomment %}
{% assign tagsArray = "" | split:"" %}

{% for story in site.stories %}
  {% assign tagsArray = tagsArray | concat: story.tags %}
  tagsArray : {{ tagsArray | inspect }}
{% endfor %}

tagsArray : {{ tagsArray | inspect }}

{% assign tagsArray = tagsArray | uniq %}

tagsArray uniq : {{ tagsArray | inspect }}

uniq在这感谢中工作,但他们不再分离。因为我们需要它们单独链接,所以如果你点击它们,它会用标签过滤。如果我在{tags | uniq | sort}上放一个标签,它会把所有单词的链接都放在上面。.uniq在这个感谢中工作,但它们不再分开。因为我们需要它们单独链接,所以如果你点击它们,它会用标签过滤。如果我在{tags | uniq | sort}上加上一个标记,它会在所有单词周围加上链接。。