2017-03-10 41 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