我正在使用 Hugo 中的 docsy 主题来创建一个文档较多的网站。我已将 easy-gallery 短代码集成到我的主题中。我使用了 Darthagnon 的 this 的更新叉子。
我正在使用叶包,我想以不必指定整个目录,而只需指定包中的文件夹的方式更改此短代码。
content/
├── branch-bundle-1
└── branch-bundle-2
├── _index.md <-- in here
├── image-gallery-1
└── image-gallery-2
因此,在 _index.md 中,我想引用短代码,然后读取 _index.md 的目录,然后将
image-galley-1
添加到该目录,以便生成一个图像库,其中包含 /content/branch-bundle-2/image-gallery-2
中的图像,只需说明 {{< gallery dir="image-gallery-2" />}}
默认情况下,短代码需要我在
/static/
中为其提供一个目录。默认情况下,它需要一个目录 /static/.../image-gallery-1
,然后我可以使用 {{ gallery dir=".../image-gallery-1"
访问该目录。所以它会在 /static/
中自动启动。
原来的短代码看起来完全像这样:
{{- if not ($.Page.Scratch.Get "figurecount") }}<link rel="stylesheet" href="{{ relURL "/css/hugo-easy-gallery.css" }}" />{{ end }}
{{- $.Page.Scratch.Add "figurecount" 1 }}
{{ $baseURL := .Site.BaseURL }}
{{- $thumbnailSize := .Get "thumbnail-size" | default "300x300" -}}
<div class="gallery caption-position-{{ with .Get "caption-position" | default "bottom" }}{{.}}{{end}} caption-effect-{{ with .Get "caption-effect" | default "slide" }}{{.}}{{end}} hover-effect-{{ with .Get "hover-effect" | default "zoom" }}{{.}}{{end}} {{ if ne (.Get "hover-transition") "none" }}hover-transition{{end}}" itemscope itemtype="http://schema.org/ImageGallery">
{{- with (.Get "dir") -}}
<!-- If a directory was specified, generate figures for all of the images in the directory -->
{{- $files := readDir (print "/static/" .) }}
{{- range $files -}}
<!-- skip files that aren't images, or that inlcude the thumb suffix in their name -->
{{- $thumbext := $.Get "thumb" | default "-thumb" }}
{{- $isthumb := .Name | findRE ($thumbext | printf "%s\\.") }}<!-- is the current file a thumbnail image? -->
{{- $isimg := lower .Name | findRE "\\.(gif|jpg|jpeg|tiff|png|bmp|webp|avif|jxl)" }}<!-- is the current file an image? -->
{{- if and $isimg (not $isthumb) }}
{{- $caption := .Name | replaceRE "\\..*" "" | humanize }}<!-- humanized filename without extension -->
{{- $linkURL := print $baseURL ($.Get "dir") "/" .Name | absURL }}<!-- absolute URL to hi-res image -->
{{- $filetype := index (findRE "[^.]+$" .Name ) 0 }}<!-- file extension of image -->
{{- $thumb := .Name | replaceRE "(\\.)" ($thumbext | printf "%s.") }}<!-- filename of thumbnail image -->
{{- $thumbexists := where $files "Name" $thumb }}<!-- does a thumbnail image exist? -->
{{- $thumbURL := print $baseURL ($.Get "dir") "/" $thumb | absURL }}<!-- absolute URL to thumbnail image -->
<div class="box">
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<div class="img" style="background-image: url('{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}');" >
<img itemprop="thumbnail" src="{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}" alt="{{ $caption }}" /><!-- <img> hidden if in .gallery -->
</div>
<figcaption>
<p>{{ $caption }}</p>
</figcaption>
<a href="{{ $linkURL }}" itemprop="contentUrl"></a><!-- put <a> last so it is stacked on top -->
</figure>
</div>
{{- end }}
{{- end }}
{{- else -}}
{{ with .Inner -}}
<!-- If no directory was specified, include any figure shortcodes called within the gallery -->
{{ . }}
{{- else }}
<!-- Generate figures for all page resources of type image. -->
{{- range $.Page.Resources.ByType "image" -}}
{{- $thumbnail := .Fill $thumbnailSize -}}
<div class="box">
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<div class="img" style="background-image: url('{{ $thumbnail.RelPermalink }}');" >
<img itemprop="thumbnail" src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" alt="{{ .Title }}" /><!-- <img> hidden if in .gallery -->
</div>
{{- with .Title -}}
<figcaption>
<p>{{ . }}</p>
</figcaption>
{{- end -}}
<a href="{{ .RelPermalink }}" itemprop="contentUrl"></a><!-- put <a> last so it is stacked on top -->
</figure>
</div>
{{- end }}
{{- end }}
{{- end }}
</div>
我尝试更改第 8 行中的
{{- $files := readDir (print "/static/" .) }}
,特别是 readDir
之后的部分,但这不起作用。
我尝试在
.Dir
内使用.Page.Dir
、.File.Page.Dir
、(...)
之类的东西,并且尝试通过如下方式定义一个新变量:{{ $variable := .Dir }}
然后我尝试将第8行更改为某些内容就像{{- $files := readDir ("$variable" .)}}
。
不出所料,这不起作用。实际上,我对 GoLang 及其语法一无所知。我想我也必须更改第 8 行以下的一些代码,但我不确定到底在哪里以及在什么地方。
上面的更改给我带来了各种错误。
You cannot use X in Y type of expressions
时尚中的男子气概错误。
我希望有人可以引导我对短代码进行必要的更改,以读取当前目录以使用我所在的捆绑包中的图像文件夹,只需说明捆绑包中的文件夹名称,而不需要捆绑包本身的目录。
目录与(子)部分有何区别?不是太多。这就是为什么我不会走这条路。选择静态文件夹是明智的选择。特别是当您将静态文件夹设置为资产目录时。
但是,按照您建议的方式进行是可以的。查看 https://gohugo.io/content-management/page-resources/,您会发现可以使用以下方法遍历图像:
{{ .Resources.Match "image-gallery-2/*" }}
当您将它们存储在变量中并对其进行范围/遍历时,您将获得图库中的所有图像。当然,您有一个非常复杂的短代码......但是该短代码是多用途的并且有太多行。我会摆脱该短代码并使用此策略重写它。
您需要一个如下所示的短代码调用:
{{< mygallery dir="image-gallery-2" }>>
然后是一个名为
mygallery.html
的短代码,其代码如下:
{{ range (.Resources.Match (print (.Get "dir") "/*")) }}
... output your image ...
{{ end }}