Params

Params 方法

Params 方法用於 頁面資源,它返回前置資料中定義的資源參數映射。此方法不適用於 全域遠端 資源。

範例

假設你的內容結構如下:

content/
├── posts/
│   ├── cats/
│   │   ├── images/
│   │   │   └── a.jpg
│   │   └── index.md
│   └── _index.md
└── _index.md

並且在 cats.md 的前置資料中加入以下內容:

content/posts/cats.md
     
---
resources:
- params:
    alt: Photograph of black cat
    temperament: vicious
  src: images/a.jpg
  title: Felix the cat
title: Cats
---
+++
title = 'Cats'
[[resources]]
  src = 'images/a.jpg'
  title = 'Felix the cat'
  [resources.params]
    alt = 'Photograph of black cat'
    temperament = 'vicious'
+++
{
   "resources": [
      {
         "params": {
            "alt": "Photograph of black cat",
            "temperament": "vicious"
         },
         "src": "images/a.jpg",
         "title": "Felix the cat"
      }
   ],
   "title": "Cats"
}

使用以下模板:

{{ with .Resources.Get "images/a.jpg" }}
  <figure>
    <img alt="{{ .Params.alt }}" src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
    <figcaption>{{ .Title }} is {{ .Params.temperament }}</figcaption>
  </figure>
{{ end }}

Hugo 渲染後將顯示:

<figure>
  <img alt="Photograph of black cat" src="/posts/post-1/images/a.jpg" width="600" height="400">
  <figcaption>Felix the cat is vicious</figcaption>
</figure>

有關頁面資源的更多資訊,請參閱 頁面資源 章節。