【问题标题】:How do you nest Jekyll loops when using multiple collections types使用多种集合类型时如何嵌套 Jekyll 循环
【发布时间】:2018-03-22 15:36:30
【问题描述】:

所以我设置了 3 个集合:

推荐

服务

案例研究

我有一个主页和 2 个用于服务和案例研究的模板,用于处理推荐的输出。

我想实现三件事:

  • 在案例研究模板上,使用光滑的滑块,显示来自推荐集合的 1 个“服务相关” 推荐。如果可能的话,我想通过案例研究的一些前沿问题来实现这一点,类似于类别。这样用户只需选择适合案例研究的服务推荐,例如

    testimonial: Matthew Pope

  • 在服务模板上,使用光滑的滑块,显示用户可以从中选择的该服务的多个相关推荐,可能是一个数组,因此:

    testimonials: [ Matthew Pope, Jon Vining ]

  • 在网站主页上再次使用光滑的滑块,遍历一些案例研究,但只显示案例研究的推荐部分。之所以只是对推荐集合进行迭代,是因为我想保留案例研究链接,因此单击阅读更多将带用户进入包含推荐功能的案例研究。

这是我用于推荐循环的代码,在每个模板上几乎相同

{% assign testimonials = site.testimonials | where: 'title', page.testimonials | first %}
{% for testimonial in testimonials %}
<div class="testimonial-entry">
    <p class="lead font-italic padding-1">{{ testimonial.content }}</p>
    <div class="grid-x grid-margin-x align-middle testimonial-entry__meta">
        <img class="cell medium-shrink avatar--medium margin-top-1" src="{{ testimonial.avatar }}"/>
        <p class="cell medium-auto margin-bottom-0 margin-top-1"><strong>{{ testimonial.title }}</strong><br/><small>{{ testimonial.job }}</small></p>
    </div>
</div>
{% endfor %}

这里是推荐集合中每个项目的前言:

---
title: Name
job: Job Title
avatar: "/assets/images/uploads/client-avatars/firstname-secondname-150x150.jpg"
solution: Service Name
---

Content here

我正在努力输出案例研究和服务的前端定义的推荐,我不确定如何最好地定义前端。

【问题讨论】:

    标签: jekyll


    【解决方案1】:
    {% assign testimonials = site.testimonials 
       | where: 'title', page.testimonials %}
    

    不返回任何内容,因为 page.testimonials 是一个数组,您无法比较 testimonial.title (string)page.testimonials (array)

    但是你可以测试一个数组contains是否是一个字符串:

    {% for testimonial in site.testimonials %}
        {% if page.testimonials contains testimonial.title %}
        <div class="testimonial-entry">
    ...
        </div>
        {% endif %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2022-11-14
      • 2021-10-09
      • 2016-09-28
      • 2014-01-03
      • 2014-12-31
      相关资源
      最近更新 更多