【问题标题】:Jekyll Child Page NavigationJekyll 子页面导航
【发布时间】:2013-10-19 15:36:43
【问题描述】:

我正在完成我的网站重新设计,只需要完成我的投资组合页面。我不想使用帖子作为投资组合条目,而是使用子目录/子页面:

...
work
  project
     index.html
  project-2
    index.html
  index.html
...

我想遍历列表中的这些子页面以显示在work/index.html 上。 类似于:

<ul>
  {% for page in site.work.pages %}
    <li>
      <figure>
        <img src="/img/foo.jpg" alt="foo">
      </figure>
    </li>
  {% endfor %}
</ul>

如何做到这一点?

【问题讨论】:

    标签: html jekyll liquid


    【解决方案1】:

    Jekyll 并不像您的示例那样简单地支持这一点,it is coming in 2.0 however。

    您可以在子页面的 YAML 标头中添加一个键/值对,以表明它应该出现在主索引页面上。我有一个类似的设置,用于定义哪些页面应出现在网站的主导航中。

    project/index.html 等

    ---
    group: work
    ---
    

    工作/index.html

    <ul>
    {% for node in site.pages %}
        {% if 'work' == node.group %}
        <li><a href="{{node.url}}">{{node.title}}</a></li>
        {% endif %}
    {% endfor %}
    </ul>
    

    如果您更改 if 条件来进行 URL 的子字符串匹配,您可能可以避免需要 group 属性,但这种解决方案更容易理解。

    【讨论】:

      【解决方案2】:

      如果您使用jekyll build 构建自己的页面,您可以简单地创建一个名为_plugins/page_filters.rb 的文件,其内容如下:

      module Jekyll
        module PageFilters
          def children_of(all_pages, parent)
            all_pages.select { |p| child_of?(p, parent) }
          end
      
          private
      
          def child_of?(child, parent)
            parent_path = parent["path"]
            child_path = child.path
      
            # Exclude 'index.md' from becoming a child of itself
            return false if parent_path == child_path
      
            # Remove 'index.md' from the parent path
            parent_path = parent_path.split("index.md", 2).first
      
            child_path.start_with? parent_path
          end
        end
      end
      
      Liquid::Template.register_filter(Jekyll::PageFilters)
      

      然后像这样调用children_of 过滤器:

      {% assign children = site.pages | children_of: page %}
      <ul>
      {% for child in children %}
          <li><a href="{{ child.url }}">{{ child.title }}</a></li>
      {% endfor %}
      </ul>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-12
        • 1970-01-01
        相关资源
        最近更新 更多