使用Jekyll,如何检查文件夹中是否存在文件?
问题描述:
我有一个_includes
文件夹,并且其中有一个nav
子文件夹,其中包含一个nav.html
文件.
I have an _includes
folder, and within it I have nav
subfolder containing a nav.html
file.
我一直在尝试检查nav
中是否存在nav.html
.如果是这样,我想包含该nav.html
文件.如果不存在,则我的模板将使用默认的nav
.
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 %}
希望你们能帮助我.
非常感谢
答
我无法使用现有的液体标签来找到解决方案,因此我通过使用带有自定义液体标签的插件来解决了这个问题.
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 %}
请参见 GitHub存储库