使用Jekyll如何检查文件夹中是否存在文件?

时间:2016-11-23 03:11:19

标签: jekyll liquid

我有一个_includes文件夹,其中包含nav文件的nav.html子文件夹。

我一直试图检查nav.html中是否存在nav。如果是,我想要包含nav.html文件。如果它不存在,我的模板将有一个默认的nav

这是我现在的代码:

{% for static_file in site.static_files %}
    {% if static_file.path == '/nav.html' %}
        {% include nav.html %}
    {% endif %}
{% endfor %}

希望你们能帮助我。

非常感谢提前

1 个答案:

答案 0 :(得分:0)

我无法使用现有的液体标签找到解决方案,所以我通过使用带有自定义液体标签的插件解决了这个问题。

{% capture fileExists %}{% file_exists _includes/nav/custom/nav.html %}{% endcapture %}
{% if fileExists=="true" %}
    {% include /nav/custom/nav.html %}
{% else %}
    {% include /nav/default/nav.html %}
{% endif %}

请参阅GitHub Repo