For loop Jekyll在集合';s子目录
提前感谢您提供的任何帮助 我正在尝试为集合创建嵌套导航,For loop Jekyll在集合';s子目录,for-loop,jekyll,liquid,templating,For Loop,Jekyll,Liquid,Templating,提前感谢您提供的任何帮助 我正在尝试为集合创建嵌套导航,d-foundation。我希望嵌套nav的结构基于目录结构,而不是手动创建Yaml文件,因为我希望它尽可能动态。我的目录结构的缩写版本如下所示: |-Root/ |-d-foundation/ |--|-color.md |--|-headings.md |--|-formats.md |--|--formats/ |--|--|-date.md |--|--|-time.md 我在.md文件中放置了前面的内容,以指定该文件是父文件还是
d-foundation
。我希望嵌套nav的结构基于目录结构,而不是手动创建Yaml文件,因为我希望它尽可能动态。我的目录结构的缩写版本如下所示:
|-Root/
|-d-foundation/
|--|-color.md
|--|-headings.md
|--|-formats.md
|--|--formats/
|--|--|-date.md
|--|--|-time.md
我在.md
文件中放置了前面的内容,以指定该文件是父文件还是子文件,或者如果该文件与任何内容都没有关联,则不包括这些属性
本例中的父项是d-foundation
根目录中的formats.md
:
---
parent: true
parent-name: foo
---
Child/children位于格式
目录中,具有以下属性:
---
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>
{基金会在基金会中的百分比.D基金会%}
{%如果基础。
{%endif%}
{%,如果基础。父!= nIL-%}
{{基础} }}
{site.d-foundation%中的子项为%s}
{%if child.child-of!=nil%}
{%endif%}
{%endfor%}
{%endif%}
{%endfor%}
我知道我的问题之一在于,我没有将循环的范围限制在每个父级(你能做到吗?)。结果是,如果我有几个子目录,第二个for循环将简单地打印出任何带有子属性的文件。如图所示,缩进最深的项目是子项,您可以看到重复项:
顶部部分不复制子目录的唯一原因是我只有一个父/子目录
我把自己弄得一团糟,我想知道我能做些什么,只在每个父母各自的孩子身上循环。或者,我是不是完全颠倒过来了? < p>更改内部<代码> < <代码> > <代码> {%,如果Calth.CHILDOF==Buffig.Posiv%} /代码>,用它们的子类过滤每个类别。
所以它看起来像:
<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>
{基金会在基金会中的百分比.D基金会%}
{%如果基础。
{%endif%}
{%,如果基础。父!= nIL-%}
{{基础} }}
{site.d-foundation%中的子项为%s}
{%如果儿童。
{%endif%}
{%endfor%}
{%endif%}
{%endfor%}
旁注:液体中的所有值均为真值,除nil和false外。所以你可以使用<代码>如果是基础。NIL<代码> < /P>我想如果你改变了内部的<代码>如果语句与<代码> {%如果Calth.CHILDOF==Buffy.Posiv%} /Cult> @ McCabuy就是这样!非常感谢。不客气,作为回答补充。啊,注意到。谢谢你的澄清。