使用Jekyll检查文件是否存在 [英] Check for existence of file using Jekyll

查看:51
本文介绍了使用Jekyll检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Jekyll测试文件的存在?

How can I use Jekyll to test for the existence of a file?

为澄清起见,我想运行{% if %}语句来检查是否存在与我所在页面同名的图像文件.

To clarify, I want to run an {% if %} statement to check if an image file exists with the same name as the page I am on.

在我的页面上,YAML至关重要:

On my page in the YAML front matter:

----
  reference-design: true
----

在我的布局中:

{% if page.reference-design %}
    {% assign filename = page.path | remove_first: '.html' %}
    <!-- How can I check if file actually exists? -->
    <img src="images/reference_designs/{{ filename }}.png">
{% endif %}

推荐答案

从Jekyll 2开始,所有站点文件都可以通过site.static_files获得.您可以使用它来检查文件是否存在.例如:

As of Jekyll 2, all site files are available via site.static_files. You can use this to check if a file exists. For example:

{% for static_file in site.static_files %}
    {% if static_file.path == '/favicon.ico' %}
        {% assign favicon = true %}
    {% endif %}
{% endfor %}

这篇关于使用Jekyll检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆