文件内容

用于从页面资源文件、站点资源文件和项目文件读取内容的短代码,其对重用内容十分有用,比如代码片段。

文件选择顺序

  1. 页面资源文件。
  2. 站点资源文件。
  3. 项目文件。

语法

1{{< file-content "/path/to/file" >}}

参数

name

索引名称类型必填默认值版本
#0namestring--

文件名。

示例

读取页面资源文件

1{{< file-content "data/foo.txt" >}}
bar

读取站点资源文件

assets/data/hello.js
1console.log('Hello world!')
1{{< file-content "data/hello.js" >}}

读取项目文件

static/ads.txt
1google.com, pub-7827859788508375, DIRECT, f08c47fec0942fa0
1{{< file-content "static/ads.txt" >}}
google.com, pub-7827859788508375, DIRECT, f08c47fec0942fa0

读取不存在的文件

1{{< file-content "static/invalid.txt" >}}

与其他短代码组合

Bootstrap 配置切换为例。

1{{< bs/config-toggle hugo >}}
2{{< file-content "data/hugo.yaml" >}}
3{{< /bs/config-toggle >}}

hugo.yaml

1title: Site Title

hugo.toml

1title = 'Site Title'

hugo.json

1{
2   "title": "Site Title"
3}