资源
语法
PAGE.Resources
返回值
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
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 |