GitHub Pages中的分层类别 [英] Hierarchical Categories in GitHub Pages

查看:94
本文介绍了GitHub Pages中的分层类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在GitHub页面上使用Jekyll,并且我希望具有这样的层次结构类别:

I'm using Jekyll on GitHub pages, and I want to have hierarchical categories like this:

  • 动物->哺乳动物->猫-> _posts-> housecat.md,tiger.md
  • 动物->哺乳动物->狗-> _posts-> poodle.md,doberman.md
  • 动物->爬行动物->蜥蜴-> _posts-> iguana.md,chameleon.md

我希望用户能够访问/animals并查看每个类别的每个帖子的列表.但是如果他们去/animals/mammals,他们只会看到哺乳动物.如果他们去/animals/mammals/cats,那么他们只会看到猫.

I'd like users to be able to visit /animals and see a listing of every post from every category. But if they go to /animals/mammals, they'd only see mammals. If they go to /animals/mammals/cats, then they only see cats.

我知道我可以通过在每个目录中放置一个index.html文件,然后循环遍历site.categories.mammalssite.categories.cats来手动完成此操作.

I know I can do this manually by putting an index.html file in every single directory and then looping through site.categories.mammals or site.categories.cats, for example.

但是,这似乎有点过分用力,我希望有更好的方法.如果要更改显示清单的方式,则必须在每个子类别中都进行更改.当子类别共享名称时,例如/ABC/XYZ/_posts/one.md/DEF/XYZ/_posts/two.md,我也会遇到问题.

But that seems a little bit too brute force, and I'm hoping there's a better way. If I want to change how I'm showing the listings, I'll have to change that in every single subcategory. I'll also have problems when subcategories share a name, like /ABC/XYZ/_posts/one.md and /DEF/XYZ/_posts/two.md.

我尝试遵循文章,该文章使用一个通过page.category循环的主category.html页面:

I've tried to follow this article, which uses one main category.html page that loops through page.category:

{% for post in site.categories.[page.category] %}
  <h2><a href=""></a></h2>
  <p></p>
{% endfor %}

然后,每个index.html文件都将其用作其布局.这几乎可行,但似乎仅限于一个类别,而不是多个层次类别.

Then every index.html file uses this as its layout. That almost works, but it seems limited to one category, not multiple hierarchical categories.

为分层类别创建列表时,是否存在一种不太暴力的方法?

Is there a less brute-force approach to creating listings for hierarchical categories?

推荐答案

page.categories是一个列表

page.categories is a list

https://stackoverflow.com/a/23927986

{% for cat in page.categories %}
  <h1>{{ cat }}</h1>
  <ul>
    {% for post in site.categories[cat] %}
      <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
  </ul>
{% endfor %}

摘自jekyll关于page.category的文档 http://jekyllrb.com/docs/variables /#page-variables

From jekyll's documentation about page.category http://jekyllrb.com/docs/variables/#page-variables

此帖子所属类别的列表.类别是 从_posts目录上方的目录结构派生而来.为了 例如,/work/code/_posts/2008-12-24-closures.md上的帖子将具有 此字段设置为['工作','代码'] .这些也可以在 YAML重要事项.

The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter.

您应该能够轻松地向每个文件夹添加一个简单的动态index.html,并且类别应自动分层.

You should be easily able to add a simple dynamic index.html to every folder and the categories should be hierarchical automatically.

以上操作无效.您需要将每个层次结构的类别组合视为唯一项.合并索引页面的类别,并将其与网站中的所有帖子进行比较.

The above does NOT work. You need to treat the combination of categories of each hierarchy as a unique item. Concat the index page's categories, and compare that against all the posts in the site.

/foo/bar/_posts/2016-08-01-foo-bar-test.html

/foo/bar/_posts/2016-08-01-foo-bar-test.html

---
categories:
  - foo
  - bar
title: test foo bar
---

<h2>Foo Bar</h2>

/var/bar/_posts/2016-08-01-test-var-bar.html

/var/bar/_posts/2016-08-01-test-var-bar.html

---
categories:
  - var
  - bar
title: test var bar
---

<h2>Var Bar</h2>

/foo/bar/index.html

/foo/bar/index.html

---
categories:
 - foo
 - bar
---

{% assign pagecat = page.categories | join ' ' | append: ' '%}
{% assign pagecatlen = page.categories.size %}
  <h1>{{ cat }}</h1>
  <ul>
    {% for post in site.posts %}
    {% assign postcat = '' %}
    {% for thispostcat in post.categories limit: pagecatlen %}
      {% assign postcat = postcat | append: thispostcat %}
      {% assign postcat = postcat | append: ' ' %}
    {% endfor %}

    {% if (postcat == pagecat) %}
      <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    {% endif %}
    {% endfor %}
  </ul>

对于_posts中的文件,类别是可选的,但是对于每个索引文件的首页而言,类别都是必需的.

The categories are optional for the files in _posts, but they are required for the front matter of each index file.

这篇关于GitHub Pages中的分层类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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