可以从它的引用访问页面的资源,因此可以通过非常简单的设置实现.
_index.md在content/images文件夹中创建一个文件,其简单的前端内容类似于下面的内容.
content/images/_index.md
---
title: Media Folder
---
Run Code Online (Sandbox Code Playgroud)
这将允许您images从站点上下文访问该部分中的资源并获取该页面.如果您不希望它在您发布的网站上显示为实际页面,则可以添加headless: true.
{{ with .Site.GetPage "section" "images" }}
<h2>{{ .Title }}</h2>
{{ $resources := .Resources.ByType "image"}}
{{ range $resources }}
{{ with . }}
<img style="max-width: 100%;" src="{{ .RelPermalink }}">
<br />
{{ end }}
{{ end }}
{{ end }}
Run Code Online (Sandbox Code Playgroud)
{{ with .Site.GetPage "section" "images" }}
<h2>From {{ .Title }} (images)</h2>
{{ $resources := .Resources.ByType "image"}}
{{ range $resources }}
{{ with . }}
{{ $image200x := (.Resize "200x") }}
{{ $image400x := (.Resize "400x") }}
<img src="{{ $image200x.RelPermalink }}">
<img src="{{ $image400x.RelPermalink }}">
<br />
{{ end }}
{{ end }}
{{ end }}
Run Code Online (Sandbox Code Playgroud)
这些示例显示如何images从捆绑中的另一个位置访问位置内的资源.您还可以使用.Resources.GetByPrefix "logo"直接获取图像资源的方式按名称访问单个图像.
在页面的前面,您将包含一个字段imagename: logo作为示例,然后:
{{ $page := . }}
{{ with .Site.GetPage "section" "images" }}
{{ with .Resources.GetByPrefix $page.Params.imagename }}
{{ $image200x := (.Resize "200x") }}
{{ $image400x := (.Resize "400x") }}
<img src="{{ $image200x.RelPermalink }}">
<img src="{{ $image400x.RelPermalink }}">
<br />
{{ end }}
{{ end }}
Run Code Online (Sandbox Code Playgroud)
注意:您也可以从降价处访问这些图像,但这需要像Hugo文档中那样进行短代码设置,并且我在下面的GitHub示例链接中包含了一个短代码示例.