我想列出我帖子中的所有标签,当然每个标签都只列出一个,所以一个标签中没有几个。
我尝试将其放在字符串中,以空格分隔它们,然后从字符串中循环出每个单词,并为其赋予uniq过滤器:
{% capture alltags %}
{% for story in site.stories %}
{{ story.tags | join: ' ' }}
{% endfor %}
{% endcapture %}
{% for word in alltags %}
{{ word | uniq }}
{% endfor %}
我得到了单词之间的空格,但是它们不是唯一的。 我确实需要将它们分别循环,以便可以在它们上建立链接。
答案 0 :(得分:1)
类似的事情会起作用。
{% 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 }}
答案 1 :(得分:1)
如果您尝试这样做,您将了解发生了什么事。
{% 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 }}