HUGO
Menu
GitHub 87548 stars Mastodon

Resources

返回页面资源的集合。

Syntax

PAGE.Resources

Returns

resource.Resources

Page 对象上的 Resources 方法返回页面资源的集合。页面资源是 页面捆绑包 内的文件。

要使用全局或远程资源,请参阅 resources 函数。

方法

ByType

(resource.Resources) 返回给定 媒体类型 的页面资源集合,如果未找到则返回 nil。媒体类型通常是 imagetextaudiovideoapplication 之一。

{{ 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 中有用。

模式匹配

使用 GetMatchMatch 方法,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

Last updated: January 1, 0001
Improve this page