资源
Syntax
PAGE.Resources
Returns
resource.Resources
Page
对象上的 Resources
方法返回页面资源的集合。页面资源是 页面包 中的一个文件。
要使用全局或远程资源,请参阅 resources
函数。
方法
按类型
(resource.Resources
) 返回给定 媒体类型 的页面资源集合,如果没有找到则返回 nil。媒体类型通常是 image
、 text
、 audio
、 video
或 application
之一。
{{ range .Resources.ByType "image" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
当使用全局资源而不是页面资源时,请使用 resources.ByType
函数。
获取
(resource.Resource
) 从给定路径返回页面资源,如果没有找到则返回 nil。
{{ with .Resources.Get "images/a.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
当使用全局资源而不是页面资源时,请使用 resources.Get
函数。
获取匹配项
(resource.Resource
) 从与给定 通配符模式 匹配的路径中返回第一个页面资源,如果没有找到则返回 nil。
{{ with .Resources.GetMatch "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
当使用全局资源而不是页面资源时,请使用 resources.GetMatch
函数。
匹配
(resource.Resources
) 从与给定 通配符模式 匹配的路径中返回页面资源的集合,如果没有找到则返回 nil。
{{ range .Resources.Match "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
当使用全局资源而不是页面资源时,请使用 resources.Match
函数。
挂载
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 使用不区分大小写的 通配符模式 来确定匹配项。
路径 | 模式 | 匹配 |
---|---|---|
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 |