I must have missed a breaking change, but going through the changelogs I couldn’t figure out which.
This code works for hugo 0.136.5:
{{- with $author.Resources.GetMatch "avatar" -}}
{{ $avatarimage := (.Fill "300x300").RelPermalink -}}
<img alt="{{ $author.Params.name }}" class="img img-raised" src="{{- $avatarimage -}}">
{{- end -}}
Using version 0.143.1 this is just returning an empty string. From what I saw in the docs, there isn’t a way to fetch a resource by name.
Am I correct?
irkode
February 10, 2025, 2:58pm
2
assuming $author
is a page object you can use Resources.Get with the full path in the page bundle or Resources.GetMatch with a glob pattern avatar*.jpg
which returns the first match.
if that does not work, you may want to share the stuff around. folder structure $uthor source, template
structure
content/
├── posts/
│ ├── post-1/
│ │ ├── a.jpg
│ │ └── index.md
│ └── _index.md
└── _index.md
content/posts/post-1/index.md
+++
title = 'Post 1'
[[resources]]
name = 'avatar'
src = 'a.jpg'
+++
layouts/_default/single.html
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ with $r := .Resources.GetMatch "avatar" }}
{{ with .Fill "300x300" }}
<img alt="{{ $r.Name }}" class="img img-raised" src="{{ .RelPermalink }}">
{{ end }}
{{ end }}
{{ end }}
This works with v0.136.5 and later.
@irkode yes it’s a page object, but I can’t assume the file name in the template.
I have these pages in content/authors/bruno-amaral/_index.md.
Here is the example front matter:
resources:
- name: avatar
src: bruno_amaral.png
- name: header
src: gregoryai_background.jpg
In other parts of the template, @jmooring 's example works to render the header of the current page.
Current template
{{$author := ""}}
{{ with .Params.authors }}
{{- range . }}
{{ $author = $.Site.GetPage (printf "authors/%s/" .)}}
{{- with $author.Resources.GetMatch "avatar" -}}
{{ $avatarimage := ""}}
{{- $avatarimage = (.Fill "300x300").RelPermalink -}}
<img alt="{{ $author.Params.name }}" class="img img-raised" src="{{- $avatarimage -}}">
{{- end -}}
structure
content/
├── authors/
│ └── bruno-amaral/
│ ├── _index.md
│ ├── bruno_amaral.png
│ └── gregoryai_background.jpg
├── posts/
│ ├── _index.md
│ └── post-1.md
└── _index.md
content/posts/post-1.md
---
title: Post 1
date: 2025-02-10T07:37:24-08:00
authors:
- bruno-amaral
---
content/authors/bruno-amaral/_index.md
---
title: Bruno Amaral
resources:
- name: avatar
src: bruno_amaral.png
- name: header
src: gregoryai_background.jpg
---
template
{{ range .Params.authors }}
{{ with $.Site.GetPage (printf "authors/%s/" .) }}
{{ with $r := .Resources.GetMatch "avatar" }}
{{ with .Fill "300x300" }}
<img alt="{{ $r.Name }}" class="img img-raised" src="{{ .RelPermalink }}">
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Works for me. Try it:
git clone --single-branch -b hugo-forum-topic-53483 https://github.com/jmooring/hugo-testing hugo-forum-topic-53483
cd hugo-forum-topic-53483
hugo server
1 Like
Reading this again, if you want the alt
attribute to be the author name instead of the resource name:
{{ range .Params.authors }}
{{ with $p := $.Site.GetPage (printf "/authors/%s/" .) }}
{{ with .Resources.GetMatch "avatar" }}
{{ with .Fill "300x300" }}
<img alt="{{ $p.Params.Title }}" class="img img-raised" src="{{ .RelPermalink }}">
{{ end }}
{{ end }}
{{ end }}
{{ end }}
I tried to implement your code but it still doesn’t return anything. Nor locally, or in CloudFlare pages.
Here is the resulting page, where I can fetch the header for the current page but not the avatar of the getPage function. The Dawn of Truly Personalized Learning
No:
That’s not a documented front matter field , and there’s a reason for that.
In every other content file you correctly created a custom parameter named “kind” by placing it under the params
key:
params:
kind: team-member
Must have done that when I was trying layouts. Thank you, @jmooring !
1 Like
To prevent this from happening in the future:
https://github.com/gohugoio/hugo/issues/12484
system
Closed
February 12, 2025, 6:43pm
12
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.