Jekyll中文文档__Static Files

文章介绍了Jekyll中静态文件的概念,这些文件不包含frontmatter,但可以通过配置文件的defaults属性设置默认frontmatter值。举例说明了如何为静态文件如图像添加frontmatter,并展示了如何在Liquid模板中过滤和列出这些文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

静态文件是指不包含任何 front matter 的文件。其中包括图像、PDFs和其他未渲染的内容。

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

VariableDescription

file.path

The relative path to the file, e.g. /assets/img/image.jpg

file.modified_time

The `Time` the file was last modified, e.g. 2016-04-01 16:35:26 +0200

file.name

The string name of the file e.g. image.jpg for image.jpg

file.basename

The string basename of the file e.g. image for image.jpg

file.extname

The extension name for the file, e.g. .jpg for image.jpg

========================================

请注意,在上表中,file 可以是任何内容。它是一个任意设置的变量,用于您自己的逻辑(例如for循环)。它不是全局网站或页面的变量。

Add front matter to static files

虽然不能直接将 front matter 值添加到静态文件中,但可以通过配置文件中的 defaults property 设置 front matter 的值。当Jekyll构建网站时,它将使用您设置的 front matter 值。

这是一个示例:

_config.yml 文件中,将以下值添加到 defaults 属性:

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

这假设您的Jekyll站点有一个 assets/img 的文件夹路径,其中存储了图像(静态文件)。当Jekyll构建网站时,它会将每个图像视为具有 front matter 值:image: true

假设您想列出 assets/img 中包含的所有图像资产。您可以使用此for循环来查找 static_files 对象,并获取所有具有此 front matter 属性的静态文件:

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

当你构建你的网站时,输出会列出每个符合这个 front matter 条件的文件路径。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值