Jekyll:生成器链接到静态层次结构中的所有子页面

时间:2017-01-19 04:10:52

标签: jekyll

我试图用Jekyll写一个静态网站,它有几层。生成某个部分中所有子页面的链接的最佳方法是什么?

例如,如果我有这样的网站结构:

landing
- Topic A
  - Content 1
  - Content 2
  - Content 3
- Topic B
  - Content 1
  - Content 2
  - Content 3

从主题页面创建指向每个内容页面的链接的最佳方法是什么?并且,是否有一种简单的方法可以链接到登陆的所有主题页面?

这些不是帖子,只是静态页面。如果我能做{% for topic.each %}等等,真的会很棒。并打印出链接。

2 个答案:

答案 0 :(得分:1)

我将在这里提出2种方法,你可以确定最好的"根据您的具体需求/情况,以及哪一种声音更适合他们。

首先,"帖子"和"页面"基本上只是md / html文件的集合。与每个variables相关联的yaml front matter

生成具有此结构的文件,您可以:

1。使用_posts和page.categories

  1. 将所有子文件放在_posts中(2017-01-01-只是一个占位符)

    _posts/
       - 2017-01-01-content-a-1.md
       - 2017-01-01-content-a-2.md
       - 2017-01-01-content-a-3.md
       - 2017-01-01-content-b-1.md
       - 2017-01-01-content-b-2.md
       - 2017-01-01-content-b-3.md
    
  2. 为每个文件添加适当的类别:

    2.1。对于帖子caontent-a-*添加类别:topic-a(按此顺序),在每个帖子的顶部premalink添加此行:

    ---
     layout: page                  # or any appropriate layout 
     category: topic-a
     ---

    2.2。对于帖子caontent-b-*添加类别:topic-b

  3. 设置creating a new collection以忽略日期,并通过将以下行添加到_config.yml来创建所需的结构:

    defaults:
     -
       scope:
         path: "_posts" # to all the file in posts
       values:
         permalink: /landing/:categories/:title.html # set this as default permalink value
    
  4. 你仍然可以在其前面的内容中指定每个帖子的永久链接,或者只是将永久链接添加到每个md文件夹前面的内容。

    以上将产生所需的结构。

    1. 遍历所有

      {% for entry in site.posts %}
         {% if entry.category == type-a %}
            <!-- do A stuff -->
         {% elsif entry.category == type-b %}
            <!-- do B stuff -->
         {% endif %}
      {% endfor %}
      
    2. 2。使用集合:

      它与上述类似,但不是使用已存在的_posts集合,而是从computed property开始(一个优点是您不需要添加date

      上述任何方法都会在_site

      中生成此结构
      landing/
        type-a/
          content-a-1/
            index.html
          content-a-2/
            index.html
          ...
        type-b/
          ... 
      

答案 1 :(得分:1)

我不会为此目的使用帖子(正如yaitloutou建议的那样)。我将从目录结构(解决方案1)中读取层次结构或创建两个单独的集合(解决方案2)。如果需要,您可以让解决方案2中的集合共享相同的布局。

1。使用页面

使用index.md页面创建一个目录结构,并在Jekyll veriable上调用'site.pages'来创建菜单。

index.md
topic-a/index.md
  content-1/index.md
  content-2/index.md
  content-3/index.md
topic-b/index.md
  content-1/index.md
  content-2/index.md
  content-3/index.md

并循环遍历所有页面:

<ul>
{% assign sitepages = site.pages | sort: 'order' %}
{% for sitepage in sitepages %}
  <li {% if page.url == sitepage.url %} class="active"{% endif %}>
    <a href="{{ sitepage.url }}">{{ sitepage.title }}</a>
  </li>
{% endfor %}
</ul>

如果您想要嵌套结构,可以执行like this。或者,如果您只想要主题A的结果,则可以执行以下操作:

<ul>
{% assign sitepages = site.pages | sort: 'order' %}
{% for sitepage in sitepages %}
{% if sitepage.url contains 'topic-a' %}
  <li {% if page.url == sitepage.url %} class="active"{% endif %}>
    <a href="{{ sitepage.url }}">{{ sitepage.title }}</a>
  </li>
{% endif %}
{% endfor %}
</ul>

2。使用集合(最简单的解决方案和最快的构建)

创建一个集合主题A并创建另一个集合主题B.您的配置文件应如下所示:

collections:
  topic-a:
    output: true
    permalink: /topic-a/:path/
  topic-b:
    output: true
    permalink: /topic-b/:path/

输出一个主题的项目如下:

{% assign atopics = site.topic-a | sort: 'order' %}
{% for atopic in atopics %}
  <li {% if page.url == atopic.url %} class="active"{% endif %}>
    <a href="{{ atopic.url }}">{{ atopic.title }}</a>
  </li>
{% endfor %}
</ul>

您应该使用content-1.md,content-2.md等文件创建_topic-a和_topic-b目录。

请注意,两个解决方案都有名为“order”的YML变量,用于确定项目/页面的外观顺序。这看起来像这样:

---
title: mytitle
layout: mylayout
order: 50
---
mycontent