如何使用jekyll自定义页面上项目的排序 [英] How to customize the sorting of items on a page with jekyll

查看:215
本文介绍了如何使用jekyll自定义页面上项目的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何表达我的要求,但是,基本上,我有一个具有多个类别的集合,该集合的所有文章都显示在单个页面上.它已经按类别进行了排序,但是,我试图找到一种以特定方式对这些类别进行排序的方法.我尝试为类别分配权重,并使用该权重进行排序,但这没有用.我不确定还可以尝试什么?

I'm not entirely sure how to phrase my request, but, basically, I have a single collection with multiple categories, all the articles of this collection are shown on a single page. It's already sorted by category, however, I'm trying to find a way to sort these categories in a specific fashion. I've tried assigning the categories weight and using that to sort, but it did not work. I'm not sure what else to try?

您可以找到我在GitHub上完成的所有操作的代码... https: //github.com/yndrelbosch/yndrelbosch.github.io

You can find the code for all that I've done on GitHub...https://github.com/yndrelbosch/yndrelbosch.github.io

我正在尝试创建此页面: https://yndrelbosch.github.io/tutorials/对页面上的类别使用自定义排序...

I'm trying to make this page: https://yndrelbosch.github.io/tutorials/ use a custom sorting for the categories on the page...

该页面的代码在这里:/pages/tutorials.html

The code for that page is here: /pages/tutorials.html

收藏集中的文章在这里:/_tutorials/

the articles in the collection are here: /_tutorials/

有人可以帮助我吗?

推荐答案

pages/tutorials.html 中,您的{% assign categories = site.tutorials | group_by:"category" | sort: "category-weight" %}绝对不起作用.

由于category-weight不是site.tutorials文档中的键,因此排序筛选器将自动失败.

As category-weight is not a key in site.tutorials documents, the sort filter will fail silently.

我们可以通过根据文档路径定义默认变量来避免在教程的前端设置类别.

We can avoid setting category in tutorials front matter by defining defaults variable depending on document path.

让我们更改_tutorials的组织:

Let's change _tutorials organization :

_tutorials
├── advanced
│   ├── adding-next-previous-to-blog.md
│   └── ...
├── getting-started
│   ├── getting-started-with-jekyll.md
│   └── ...
└── setup
    ├── jekyll-on-windows-2.md
    └── ...

_config.yml中添加一些默认值:

defaults:
  - ...
  - { scope: { path: "_tutorials/advanced" }, values:  { category: "Advanced" } }
  - { scope: { path: "_tutorials/getting-started" }, values:  { category: "Getting started" } }
  - { scope: { path: "_tutorials/setup" }, values:  { category: "Setup" } }

为教程类别顺序创建参考

_config.yml中,添加

# this is used to display tutorials in the right categories order
# be sure to match default category names defined in site.defaults
tutorials-categories:
  - "Setup"
  - "Getting started"
  - "Advanced"

请注意,将类别顺序从pages/tutorials.html移到_config.yml是为了集中配置.它有助于在默认设置和类别顺序之间保持同步.

Note that moving categories order from pages/tutorials.html to _config.yml is made to centralize configuration. It helps to stay in synch between defaults settings and categories order.

pages/tutorials中,我们可以执行以下操作:

In pages/tutorials, we can do something like :

{% assign categories = site.tutorials | group_by:"category" %}

{% comment %} ++++++++++++++++++++++++++
We loop site.tutorials-categories to sort categories in a defined order
++++++++++++++++++++++++++ {% endcomment %}
{% for category in site.tutorials-categories %}

  {% assign current-category = categories | where:"name", category | first %}
  {% assign category-tutorials = current-category.items %}

  <h2>{{ category }}</h2>
  <ul>
  {% for item in category-tutorials %}
    <li><a href="{{ site.baseurl }}{{ item.url }}">{{ item.title }}</a></li>
  {% endfor %}
  </ul>
{% endfor %}

这篇关于如何使用jekyll自定义页面上项目的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆