【问题标题】:Display a menu with distinct categories using Jekyll使用 Jekyll 显示具有不同类别的菜单
【发布时间】:2015-05-29 10:32:10
【问题描述】:

我想显示一个仅显示不同类别的菜单。

假设有以下结构:

_folder1

  1. com1.html
  2. com2.html
  3. com3.html

现在,让我只关注三个文件。

对于 _folder1 中包含的每个文件,您都有以下 YAML MATTER

标题:1 文件
类型:y
项目:1

标题:2文件
类型:y
项目:1

标题:3文件
类型:y
项目:2

现在,我想展示以下列表:

项目

  • 1
  • 2

我不想有双 1。

在 Jekyll 中实现它的最佳实践是什么?

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    这是可能的,但你需要一些非常丑陋的字符串操作技巧来实现它。

    据我所知,Liquid 中没有合适的方法来自己创建数组。
    因此,以下 90% 的解决方案包括滥用字符串以创建数组。

    <!-- Step 1: create an array with all projects (with duplicates) -->
    
    {% for page in site.pages %}
        {% if page.project %}
            {% capture tmp %}{{ tmp }}#{{ page.project }}{% endcapture %}
        {% endif %}
    {% endfor %}
    
    {% assign allprojects = tmp | remove_first: '#' | split: '#' | sort %}
    
    
    <!-- Step 2: create an array of unique projects (without duplicates) -->
    
    {% for project in allprojects %}
        {% unless tmp2 contains project %}
            {% capture tmp2 %}{{ tmp2 }}#{{ project | strip }}{% endcapture %}
        {% endunless %}
    {% endfor %}
    
    {% assign uniqueprojects = tmp2 | remove_first: '#' | split: '#' | sort %}
    
    
    <!-- Step 3: display unique projects -->
    
    <h1>Projects:</h1>
    <ul>
    {% for project in uniqueprojects %}
        <li>{{project}}</li>
    {% endfor %}
    </ul>
    

    最后,第 3 步将生成以下 HTML...完全按照要求:

    <h1>Projects:</h1>
    <ul>
        <li>1</li>
        <li>2</li>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2013-06-03
      • 2023-03-27
      • 1970-01-01
      • 2018-07-09
      相关资源
      最近更新 更多