2017-03-10 42 views
2

预先感谢您提供的任何帮助!Jekyll针对集合子目录的嵌套循环问题

我正在尝试为集合创建嵌套导航,d-foundation。我希望将目录结构中的嵌套导航结构基于该结构,而不是手动创建Yaml文件,因为我希望它尽可能具有动态性。我的目录结构的简化版本是这样的:

|-Root/ 
|-d-foundation/ 
|--|-color.md 
|--|-headings.md 
|--|-formats.md 
|--|--formats/ 
|--|--|-date.md 
|--|--|-time.md 

我把前面的问题在.md文件来指定,如果该文件是父母或儿童,或不包括任何这些属性,如果该文件不是与任何事物相关。

家长在这个例子中的d-foundationformats.md

--- 
parent: true 
parent-name: foo 
--- 

儿童/子女都在formats目录中的属性:

--- 
child-of: foo 
--- 

然后,我在尝试第一个循环顶级文件,检测文件是否为父级,然后循环后续子文件:

<ul class="design-subnav"> 
    {% for foundation in site.d-foundation %} 
    {% if foundation.child-of == nil and foundation.parent == nil %} 
    <li><a href="{{ foundation.url }}">{{ foundation.title }}</a></li> 
    {% endif %} 

    {% if foundation.parent != nil %} 
    <li><span>{{ foundation.title }}</span> 
     <ul> 
      {% for child in site.d-foundation %} 
      {% if child.child-of != nil %} 
      <li><a href="{{ child.url }}">{{ child.title }}</a></li> 
      {% endif %} 
      {% endfor %} 
     </ul> 
    </li> 
    {% endif %} 
    {% endfor %} 
</ul> 

我知道[其中一个]我的问题在于我没有将循环的范围限制到每个父母(你可以这么做吗?)。结果是,如果我有几个子目录,第二个for循环将只打印出任何具有child-of属性的文件。在这里看到,该项目缩进最远的是孩子,你可以看到重复:

A screen shot showing the loop iterating over any children in the collection, not just limited to a parent

顶部部分不重复孩子的唯一原因是,我只有一个父/子目录。

我已经把自己纠缠在这里,我想知道我能做些什么来让每个父母的孩子都能循环。还是我以完全倒退的方式谈论这个问题?

+0

我认为它会解决它,如果你改变内部'if语句'{%if child.child-of == foundation.parent%}' – marcanuy

+0

@marcanuy就是这样!非常感谢。 – cferg

+0

不客气,将其添加为答案。 – marcanuy

回答

1

更改内部if{% if child.child-of == foundation.parent %}以筛选每个类别及其子类别。

因此,它看起来像:

<ul class="design-subnav"> 
    {% for foundation in site.d-foundation %} 
    {% if foundation.child-of == nil and foundation.parent == nil %} 
    <li><a href="{{ foundation.url }}">{{ foundation.title }}</a></li> 
    {% endif %} 

    {% if foundation.parent != nil %} 
    <li><span>{{ foundation.title }}</span> 
     <ul> 
      {% for child in site.d-foundation %} 
      {% if child.child-of == foundation.parent %} 
      <li><a href="{{ child.url }}">{{ child.title }}</a></li> 
      {% endif %} 
      {% endfor %} 
     </ul> 
    </li> 
    {% endif %} 
    {% endfor %} 
</ul> 

旁注:在液体中的所有值都只是 truthy。所以你可以使用if foundation.child-of而不是if foundation.child-of != nil

+0

啊,指出。感谢您的澄清。 – cferg