eman
October 17, 2019, 5:04am
1
Hi,
I’m having some issues with getting an image resource.
This works inside a shortcode and it does print the image link and both the images
{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
{{ $background := $imgPage.Resources.GetMatch "image/background.jpg" }}
<pre>{{ .background.RelPermalink }}</pre>
<img src={{ .background.RelPermalink }} alt="background image">
{{ $resized := $background.Resize "400x" }}
<img src={{ .resized.RelPermalink }} alt="resized image">
{{ end }}
But when I replace the literal sting “image/background.jpg” with value from a front matter like this {{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
it gives me an error “at <$background.Resize>: nil pointer evaluating resource.Resource.Resize” pointing at {{ $resized := $background.Resize “400x” }}
{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
{{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
<pre>{{ .background.RelPermalink }}</pre>
<img src={{ .background.RelPermalink }} alt="background image">
{{ $resized := $background.Resize "400x" }}
<img src={{ .resized.RelPermalink }} alt="resized image">
{{ end }}
doing
{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
<pre>{{ .pathname_of_background }}</pre>
{{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
<pre>{{ $background | jsonify }}</pre>
{{ end }}
displays
image/background.jpg
{}
I tried using printf
{{ $background := $imgPage.Resources.GetMatch (printf “%s” .pathname_of_background) }}
but I still get the same error
So is there a way to get a resource by using data from front matter instead of using a string literal?
Since this code is going in a shortcode I need it to be flexible.
bep
October 17, 2019, 6:56am
2
I’m pretty sure that your examples should just work, which mans that there is “something else going” on. Maybe some whitespace? What do you see if you do “
{{ printf “%q” .pathname_of_background }} ”
eman
October 17, 2019, 7:08am
3
I get the value of .pathname_of_background but in quotes
“image/background.jpg”
I even tried
{{ $background := $imgPage.Resources.GetMatch (printf “%q” .pathname_of_background) }}
just in case but i get the same error.
just in case it might help. my hugo version is
Hugo Static Site Generator v0.56.1-0AD218AF linux/amd64 BuildDate: 2019-07-28T14:51:54Z
bep
October 17, 2019, 7:17am
4
OK; my best guess then is that when you do
image/background.jpg
{}
It’s not actually nil.
Try to drop the jsonify.
eman
October 17, 2019, 7:32am
5
when i do this
<div><pre>{{ .pathname_of_background }}</pre></div>
{{ $background := $imgPage.Resources.GetMatch (printf "%s" .pathname_of_background) }}
<div><pre>{{ $background }}</pre></div>
I get
image/background.jpg
Resource(image: image/background.jpg)
That’s good. but when i try to get the permalink (i need to put this in src of img tag) and immediately added this on the next line
<div><pre>{{ $background.Permalink }}</pre></div>`
I again get this error
at <$background.Permalink>: nil pointer evaluating resource.Resource.Permalink
bep
October 17, 2019, 7:54am
6
OK, now I think I understand.
I suspect the problem you have is that you have “many pages”, and some of them have params with a param that matches an image.
What you need to do is:
{{ $background := $imgPage.Resources.GetMatch (printf "%s" .pathname_of_background) }}
{{ with $background }}
<div><pre>{{ .RelPermalink }}</pre></div>
{{ end }}
1 Like
eman
October 17, 2019, 7:58am
7
This works.
Thanks a lot. I’ve been working on this since yesterday trying to find a way to work around it.
1 Like
bep
October 17, 2019, 8:24am
8
bep:
{{ with $background }}
Also note that you can do something like this:
{{ $background := $imgPage.Resources.GetMatch "foo.jpg" | default $defaultBackground }}
To fall back to a default if not found.
1 Like
eman
October 17, 2019, 8:28am
9
Thanks. that is worth considering. specially when I have to do some image image processing before I add it in html