Using Jekyll how can I check if file exist in my folder?

谁说胖子不能爱 提交于 2019-12-12 03:46:47

问题


I have an _includes folder, and within it I have nav subfolder containing a nav.html file.

I've been trying to check if nav.html exists in nav. If it does, I want to include that nav.html file. If it doesnt exist, there will be a default nav for my template.

Here's the code I have right now:

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

Hope you guys can help me out.

Many thanks in advance


回答1:


I am not able to find the solution using existing liquid tags so I solved this by using a plugin with a custom liquid tag.

{% 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 %}

See GitHub Repo



来源:https://stackoverflow.com/questions/40755367/using-jekyll-how-can-i-check-if-file-exist-in-my-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!