【问题标题】:Is possible to group items according to a front matter variable in Jekyll?可以根据 Jekyll 中的前事变量对项目进行分组吗?
【发布时间】:2019-05-05 01:16:24
【问题描述】:

我有一个城市列表,每个城市都包含一些商店。我想遍历每个城市,列出其中包含的商店,一旦循环列表所有商店都继续到另一个城市。

示例 html:

<div class="card-group">
  <h5 class="heading heading-5">City 1</h5>
   <div class="card card-store">
     <h6 class="heading heading-6"><a href="">Store 1</a></h5>
   </div>
   <div class="card card-store">
     <h6 class="heading heading-6"><a href="">Store 2</a></h5>
   </div>
 </div>
<div class="card-group">
 <h5 class="heading heading-5">City 2</h5>
 <div class="card card-store">
  <h6 class="heading heading-6"><a href="#">Store 3</a></h5>
 </div>
</div>

目前,我正在为每个商店设置一个包含一个文件的集合,前面的内容设置如下:

---
title: Store 1
city: City 1
---

我试图通过比较“City”变量的 if 条件来解决这个问题。城市的数量是有限的,但商店会增加。我不确定这种方法是否正确,或者我是否应该为每个城市创建一个文件夹并使用类似“page.collection”变量的东西。 最终项目将部署在无头 CMS 中。

【问题讨论】:

标签: yaml jekyll liquid


【解决方案1】:

好吧,这不是最优雅的解决方案,但它确实有效!我所做的是:

  1. 创建 _cities 和 _stores 集合。
  2. 在商店中创建一个“城市”前台变量。
  3. 循环播放:

    {% for city in site.cities %}
     <h5 class="heading heading-5"> {{ city.title }} </h5>
     {% for store in site.stores %}
      {% if city.title == store.city %}
         ##DO STUFF
      {% endif %}
     {% endfor %}
    {% endfor %}
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    相关资源
    最近更新 更多