Resources
Resources
方法在 Page
物件上返回一個頁面資源集合。頁面資源指的是[頁綑包內的檔案]。
若要處理全域或遠端資源,請參見resources
函數。
方法
ByType
(resource.Resources
) 返回特定媒體類型的頁面資源集合,若未找到則為 nil。媒體類型通常是 image
、text
、audio
、video
或 application
。
{{ range .Resources.ByType "image" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
若要處理全域資源而非頁面資源,請使用 resources.ByType
函數。
Get
(resource.Resource
) 根據提供的路徑返回頁面資源,若未找到則為 nil。
{{ with .Resources.Get "images/a.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
若要處理全域資源而非頁面資源,請使用 resources.Get
函數。
GetMatch
(resource.Resource
) 返回匹配指定glob 模式的首個頁面資源,若未找到則為 nil。
{{ with .Resources.GetMatch "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
若要處理全域資源而非頁面資源,請使用 resources.GetMatch
函數。
Match
(resource.Resources
) 返回符合指定glob 模式的頁面資源集合,若未找到則為 nil。
{{ range .Resources.Match "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
若要處理全域資源而非頁面資源,請使用 resources.Match
函數。
模式匹配
使用 GetMatch
和 Match
方法時,Hugo 使用不區分大小寫的 glob 模式 進行匹配。
Path | Pattern | Match |
---|---|---|
images/foo/a.jpg |
images/foo/*.jpg |
true |
images/foo/a.jpg |
images/foo/*.* |
true |
images/foo/a.jpg |
images/foo/* |
true |
images/foo/a.jpg |
images/*/*.jpg |
true |
images/foo/a.jpg |
images/*/*.* |
true |
images/foo/a.jpg |
images/*/* |
true |
images/foo/a.jpg |
*/*/*.jpg |
true |
images/foo/a.jpg |
*/*/*.* |
true |
images/foo/a.jpg |
*/*/* |
true |
images/foo/a.jpg |
**/*.jpg |
true |
images/foo/a.jpg |
**/*.* |
true |
images/foo/a.jpg |
**/* |
true |
images/foo/a.jpg |
** |
true |
images/foo/a.jpg |
*/*.jpg |
false |
images/foo/a.jpg |
*.jpg |
false |
images/foo/a.jpg |
*.* |
false |
images/foo/a.jpg |
* |
false |