过滤子页面并在 Jekyll 中添加 类

Filter subpages and add classes in Jekyll

我正在使用节点排序来确定 Jekyll 子文件夹中的页面。此图显示我的文件夹结构:

假设我在 http://localhost/grain/ahvsuperb/ 有一个页面(如下所示 index.html)。导航到此页面将显示添加到 link 的 class on。效果不错。

我 运行 遇到麻烦的地方是当我导航到 http://localhost/grain/ahvsuperb/features/ 之类的页面时。当我导航到该页面时, indexfeatures 都添加了 on class。关于如何使用 Liquid 过滤掉它的任何见解?

<div class="content_nav">
  <ul class="content_nav">
    {% assign sorted_pages = site.pages | sort: 'weight' %}
    {% for node in sorted_pages %}
      {% if node.title != nil and node.url != '/' %}
      {% capture nodediff %}{{ page.url | remove:node.url }}{% endcapture %}
      <li><a {% if nodediff != page.url %}class="on" {% endif %}href="{{node.url}}">{{node.title}}</a></li>
      {% endif %}
    {% endfor %}
    <li style="float:right"><a class="brochured" href="javascript:void(0);" rel="#brochuredl">Brochure</a></li>
    <li style="float:right"><a class="quoted" href="#">Get a Quote</a></li>
  </ul>
</div>

谢谢!

您的 {% capture nodediff %}{{ page.url | remove:node.url }}{% endcapture %} 然后 {% if nodediff != page.url %} 没有返回预期的页面确定。

这将完成工作:

<div class="content-nav">
  <ul class="content-nav">
    {% assign sorted_pages = site.pages | sort: 'weight' %}
    {% for node in sorted_pages %}
      {% if node.title != nil and node.url != '/' %}
      <li><a {% if node.url == page.url %}class="on" {% endif %}href="{{node.url}}">{{node.title}}</a></li>
      {% endif %}
    {% endfor %}
    <li style="float:right"><a class="brochured" href="javascript:void(0);" rel="#brochuredl">Brochure</a></li>
    <li style="float:right"><a class="quoted" href="#">Get a Quote</a></li>
  </ul>
</div>