文件內容

用於從頁面資源文件、站點資源文件和項目文件讀取內容的短代碼,其對重用內容十分有用,比如代碼片段。

文件選擇順序

  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}