【问题标题】:Listing Jekyll Collection pages by tags按标签列出 Jekyll Collection 页面
【发布时间】: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


    【解决方案1】:

    试试这个:

    {% assign tags =  site.note | map: 'tags' | join: ','  | split: ',' | uniq %}
    {% for tag in tags %}
      <h3>{{ tag }}</h3>
      <ul>
      {% for note in site.note %}
        {% if note.tags contains tag %}
        <li><a href="{{ site.baseurl }}{{ note.url }}">{{ note.title }}</a></li>
        {% endif %}
      {% endfor %}
      </ul>
    {% endfor %}
    

    【讨论】:

      【解决方案2】:

      David Jacquel 的回答中提到的tags 赋值方法可以简化为:

      {% assign tags =  site.note | map: 'tags' | uniq %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        相关资源
        最近更新 更多