【问题标题】:Generate headings from a Jekyll Collection item's front-matter从 Jekyll Collection 项目的 front-matter 生成标题
【发布时间】:2018-01-08 17:27:36
【问题描述】:

如下所示,这是一个相当简单的集合设置,其中包含位于根级别的 _events 文件夹中的事件的无序列表(输出设置为 false)。我不想在布局中使用{{ page.title }},而是希望标题读取为月份。

例如,如果我在 _events 集合中添加一个新项目并在前面的内容中包含 month: January,我希望它生成一个“一月”标题,并将该无序列表放在标题下方。同样,如果我添加一个新的集合项并在 front-matter 中包含 month: February,我想生成一个“February”标题,该标题下方有一个新的无序列表。

我知道我需要重新安排标题所在的位置,我想我需要某种类型的 if 声明,但我遇到了障碍。这个我想不通。

Jekyll Collections 可以做到这一点吗?

布局

{% include root.html %}

<div class="flex-main">
    {% include header.html %}
    <main class="o-main" role="main">
        <div class="o-grid-six">
          <h1 class="f1 o-grid-six__child c-headline">{{ page.title }}</h1>
        </div>
        {{ content }}
    </main>
</div>

<div class="flex-footer">
    {% include footer.html %}
</div>

查看(事件)

---
layout: events
title: Events
order: 2
---
<ul class="c-event-list">
  {% for events in site.events %}
  <li class="c-event-list__flag">
      <div class="c-event-list__flag--left">
        <div class="c-event-list__calendar">
          <span class="c-event-list__day">{{ events.weekday }}</span>
          <span class="f2 c-event-list__date">{{ events.date-number }}</span>
        </div>
        <img class="c-event-list__image" src="{{ p.url | prepend: site.baseurl }}{{ events.thumb }}" alt="">
      </div>
      <div class="c-event-list__flag--body">
        <div class="c-event-list__content">
          <h3 class="f2 c-event-list__title">{{ events.title }}</h3>
          <p>{{ events.description }}</p>
          <a class="c-button u-mt-2" href="{{ events.link }}">Sign Up</a>
        </div>
      </div>
  </li>
  {% endfor %}
</ul>

收藏品(一个事件)

---
layout: default
thumb: /images/event__screen-printing-basics.jpg
link: https://www.eventbrite.com/e/screen-printing-basics-tickets-41882948025
title: Screen Printing Basics
weekday: Wednesday
month: January
date-number: '10'
description: Join us for an after hours session to learn everything you need to know about screen printing in the Make Lab. We will be coating screens with emulsion, printing artwork onto transparencies, burning the image into the screen, washing out the stencil, mixing ink, prepping our work station, registering the paper, and finally pulling prints. The artwork always varies, so you’ll leave with a one-of-a-kind screen printed poster that you made yourself!
---

界面

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    实现这一点的最佳方法是使用真正的dateTimegroup_by_exp 过滤器。

    日期时间

    ---
    layout: default
    thumb: /images/event__screen-printing-basics.jpg
    link: ...
    title: Screen Printing Basics
    description: ...
    date: 2018-03-21 20:00
    ---
    

    请注意,由于某种原因,date: 2018-03-21 不起作用。

    group_by_exp 过滤器

    简化的打印逻辑可以是:

    {% assign eventsByYear = site.events | group_by_exp:"event", "event.date | date: '%Y'" %}
    {% for year in eventsByYear %}
      <h1>{{ year.name }}</h1>
      {% assign eventsByMonth = year.items | group_by_exp:"event", "event.date | date: '%B'" %}
      {% for month in eventsByMonth %}
        <h2>{{ month.name }}</h2>
        <ul>
          {% for event in month.items %}
            <li>{{ event.title }}-{{ event.date | date: "%A, %B %d, %Y"}}</li>
          {% endfor %}
        </ul>
      {% endfor %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 2015-05-12
      • 2015-08-17
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多