Resources
返回頁面資源的集合。
Syntax
PAGE.Resources
Returns
resource.Resources
Page 對象上的 Resources 方法返回頁面資源的集合。頁面資源是 頁面捆綁包 內的文件。
要使用全局或遠程資源,請參閱 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 函數。
Mount
New in v0.140.0(ResourceGetter) 將給定資源從兩個參數 base(string)掛載到給定目標路徑(string)並返回實現 Get 的對象。請注意,目標中的前導斜槓標記絕對路徑。相對目標路徑允許您相對於另一組資源掛載資源,例如 頁面捆綁包:
{{ $common := resources.Match "/js/headlessui/*.*" }}
{{ $importContext := (slice $.Page ($common.Mount "/js/headlessui" ".")) }}此方法目前僅在 js.Batch 中有用。
模式匹配
使用 GetMatch 和 Match 方法,Hugo 使用不區分大小寫的 glob 模式 確定匹配。
| 路徑 | 模式 | 匹配 |
|---|---|---|
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 |