静态文件(Static Files)

除了用于渲染和转换的内容之外,我们还可以使用静态文件

静态文件不包含任何 YAML 头信息,譬如图片、PDF 和其他不必渲染的内容。

它们在 Liquid 中可以通过 site.static_files 访问,还包括以下元数据:

变量 说明

file.path

文件的相对路径,如:/assets/img/image.jpg

file.modified_time

文件的最后修改时间,如:2016-04-01 16:35:26 +0200

file.name

文件名称(带扩展名),如:文件image.jpg对应image.jpg

file.basename

文件名称(不带扩展名) 如:文件image.jpg对应image

file.extname

文件的扩展名,如 image.jpg 中的 .jpg

Note that in the above table, file can be anything. It’s an arbitrarily set variable used in your own logic (such as in a for loop). It isn’t a global site or page variable.

Add front matter to static files

Although you can’t directly add front matter values to static files, you can set front matter values through the defaults property in your configuration file. When Jekyll builds the site, it will use the front matter values you set.

Here’s an example:

In your _config.yml file, add the following values to the defaults property:

defaults:
  - scope:
      path: "assets/img"
    values:
      image: true

This assumes that your Jekyll site has a folder path of assets/img where you have images (static files) stored. When Jekyll builds the site, it will treat each image as if it had the front matter value of image: true.

Suppose you want to list all your image assets as contained in assets/img. You could use this for loop to look in the static_files object and get all static files that have this front matter property:

{% assign image_files = site.static_files | where: "image", true %}
{% for myimage in image_files %}
  {{ myimage.path }}
{% endfor %}

When you build your site, the output will list the path to each file that meets this front matter condition.