生成给定类别中的页面列表(非帖子) [英] Generating a list of pages (not posts) in a given category

查看:62
本文介绍了生成给定类别中的页面列表(非帖子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Jekyll用作网站(而不是博客)的静态生成器,并且希望在索引页面上自动生成所有页面的列表.具体来说,我想拥有不同的类别,并分别列出每个类别中的所有文章. 这里是我正在描述的示例,如果您在跟踪时遇到困难.在Jekyll中有什么方法可以做到这一点(例如GitHub页面)?我看过变量文档页面,但这似乎是博客文章格式所特有的.

I am using Jekyll as a static generator for a website (not a blog), and I want to have an automatically generated list of all pages on my index page. Specifically, I want to have different categories and list all articles in each category separately. Here's an example of what I'm describing, if you're having trouble following. Is there any way to do this in Jekyll (e.g. GitHub pages)? I've seen the variables documentation page but that seems specific to the blog post format.

推荐答案

在构建自己的网站时,我遇到了同样的问题,并且发现了一个(IMHO)简单且健壮的解决方案.希望这对希望做类似事情的其他人有用.

While building my own site I came across this very same problem, and I have found an (IMHO) easy and robust solution. Hopefully this is useful for anyone else wishing to do something similar.

给出网站上的部分页面(而非帖子),根据类别将其列出在标题下.例如:给定一组我们认为是 resource 页面的页面(或参考页面,或者您要显示的页面的任何逻辑分组),我们希望将它们列出在其类别下(例如.代码,解释等).

Given a subset of pages (not posts) on the site, list them under headings based on their categories. For example: given a set of pages which we consider resource pages (or reference pages, or whatever logical grouping of pages that you want to display are), we want to list them under their categories (ex. code, explanation, et cetera).

要获得所需的行为,我们必须在三个地方进行修改:

To get the behaviour that we want, we have to make modifications in three places:

  • _config.yml
  • resources.md
  • resource-file-X.md
  • _config.yml
  • resources.md
  • resource-file-X.md

_config.yml中,我们必须添加将出现在资源文件中的所有类别/关键字/标签(或任何您想调用的名称)的列表.这是我的东西:

In _config.yml, we must add a list of all of the categories/keywords/tags (or whatever you want to call it) that will appear in the resource files. Here is what I have in mine:

category-list: [code, editors, math, unix]

您可以调用任何变量,我选择了category-list,只需确保在resource.md文件中使用相同的变量即可.

You can call the variable anything, I chose category-list, just make sure that you use the same variable in the resource.md file.

注意:将项目放置在列表中的顺序就是它们在resource.md页上列出的顺序.

Note: The order that you place the items in the list is the order they will be listed on the resource.md page.

这些是要在resources.md页上建立索引并链接到的文件.您需要做的就是在每个文件的顶部添加两个文件变量.第一个是表明此文件是资源文件.

These are the files that you want to have indexed and linked to on the resources.md page. All that you need to do is add two file variables to the top of each of these files. The first is to indicate that this file is a resource file.

resource: true

第二个是指示您希望此文件建立索引的类别.您可以根据需要在多个类别下对其进行索引,并且如果您希望页面不被索引,请将列表保留为空白.我对在C语言中正确处理EINTR的参考具有以下类别:

The second is to indicate what categories you want this file to be indexed under. You can index it under as many categories as you would like, and if you want a page un-indexed, leave the list blank. My reference for proper EINTR handling in C has the following categories:

categories: [code, unix]

resources.md

此文件将根据各自的类别生成页面列表.您需要做的就是将以下代码添加到该文件(或您希望列表位于其中的任何文件):

resources.md

This is the file that will generate the list of pages based on their respective categories. All you need to do is add the following code to this file (or whatever file you want the list to be on):

{% for cat in site.category-list %}
### {{ cat }}
<ul>
  {% for page in site.pages %}
    {% if page.resource == true %}
      {% for pc in page.categories %}
        {% if pc == cat %}
          <li><a href="{{ page.url }}">{{ page.title }}</a></li>
        {% endif %}   <!-- cat-match-p -->
      {% endfor %}  <!-- page-category -->
    {% endif %}   <!-- resource-p -->
  {% endfor %}  <!-- page -->
</ul>
{% endfor %}  <!-- cat -->

代码分解

快速解释一下它是如何工作的:

Code Breakdown

Just a quick explanation of how this works:

  • 浏览_config.yml中指定的每个类别.
  • 显示具有该类别名称的标题.
  • 为属于该类别的页面创建无序列表.
  • 浏览网站上的页面.
  • 如果页面是文件变量resource所示的资源文件,则对于文件所属的每个类别,如果其中一个与列出的当前类别相匹配,显示该页面的链接.
  • Loop through each of the categories specified in _config.yml.
  • Display a heading with that category name.
  • Start an unordered list for the pages that belong in that category.
  • Loop through the pages on the site.
  • If the page is a resource file as indicated by the file variable resource, then for each of the categories that the file belongs to, if one of them matches the current category being listed, display a link to that page.

注意:资源文件中_config.ymlcategories中的变量category-list可以随意调用,只要确保在生成列表的文件中使用相同的变量即可.

Note: the variables category-list in _config.yml and categories in the resource files can be called whatever you want, just make sure that you use the same variables in the file generating the list.

另一个注意事项:修改_config.yml时,必须完全重新启动Jekyll,即使具有--watch选项,也必须停止并重新启动它.我花了一段时间才弄清楚为什么我的更改没有生效!

Another Note: When you modify _config.yml, you have to completely restart Jekyll, even if you have the --watch option, you have to stop and restart it. It took me a while to figure out why my changes weren't taking effect!

您可以在我的网站的资源页面上看到最终产品,尽管我只是今天将它们放在一起,以便在撰写本文时还远远不够,但是如果需要,可以在主页上查看我的简历.

You can see the final product on the resources page on my site, although I just put this together today so at the time of this writing, it's far from complete, but you can check out my bio if you want on the home page.

希望这会有所帮助!

这篇关于生成给定类别中的页面列表(非帖子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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