【发布时间】:2016-08-25 19:59:40
【问题描述】:
我正在尝试创建按标签分组的集合中的页面列表。我知道这很容易通过 Jekyll 的内置 site.tags 功能实现,我可以(并且已经)通过以下方式实现:
<h3>Tags</h3>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<h4><a name="{{t | downcase | replace:" ","-" }}"></a><a class="internal" href="/tagged/#{{t | downcase | replace:" ","-" }}">{{ t | downcase }}</a></h4>
<ul>
{% for post in posts %}
{% if post.tags contains t %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
<span class="date">{{ post.date | date: "%B %-d, %Y" }}</span>
</li>
{% endif %}
{% endfor %}
</ul>
<hr>
{% endfor %}
为了得到这个:
我想在名为note 的集合中复制site.tags 函数。我的集合中的标签像帖子一样被分组,例如在 YAML 标头中使用 tags: [urban growth, California, industrialization]。但我想用 Collection 来代替它。我几乎可以通过以下方式实现我想要的:
{% assign collection = site.note | group_by: "tags" | uniq %}
{% for group in collection %}
{% comment %} This is super hacky and I don't like it {% endcomment%}
<h3>{{ group.name | replace: '"', '' | replace: '[', '' | replace: ']', '' }}</h3>
<ul>
{% for item in group.items %}
<li><a href="{{ item.url | prepend: site.baseurl | prepend: site.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>
{%endfor%}
但正如您可能看到的那样,这并没有将标签分成不同的组;相反,YAML 中的每组标签都被视为单个大标签。关于构建唯一标签数组并在其下列出收藏页面的任何指针?
【问题讨论】:
标签: jekyll