Works for all pictures but fails there: at <$medium.RelPermalink>: can’t evaluate field RelPermalink in type string

Why does that warning fails ?
I erased my image folder by mistake, restored it, and suddenly there’s that. This is a version from a backup so I don’t understand. It worked just before.

{{- $img := "" -}}
{{ if hasPrefix .Destination "http" }}
	{{ $img = resources.GetRemote .Destination }}
{{ else }}
	{{ $img = resources.Get .Destination }}
{{end}}
{{ warnf $img}}

The error is:

 template: _default/_markup/render-image.html:14:9:
executing "_default/_markup/render-image.html" at <$img>:
wrong type for value; expected string; got *resources.resourceAdapter

trying to get the .Width results in :
Error: Error building site: "/home/drm/WEBSITE/content/docs/Love/homo_nazi_en.md:1:1": "_default/_markup/render-image.html" at <$img.Width>: wrong type for value; expected string; got int
I am not using minify.

Because it expects a string as its first argument. Not sure what you want to “warn about”, but you could try

{{ warnf "%v" $img}}

It is this line that complains:

<img src=“{{ $medium.RelPermalink }}” width={{ $medium.Width }} height={{ $medium.Height }} loading=“{{if .Attributes.loading}}{{.}}{{else}}lazy{{end}}” alt=“{{ default (.Title|markdownify) .Text }}” {{ with .Title }}title=“{{ markdownify . }}” {{end }} />

With:

execute of template failed: template: _default/_markup/render-image.html:66:11: executing “_default/_markup/render-image.html” at <$medium.RelPermalink>: can’t evaluate field RelPermalink in type string

I stressed that it has created three processed versions already, that one:

{{ $small = $img.Resize "604x q90" }}

and that one:

{{- $img_reduced_quality := $img.Resize (print $img.Width "x q90") -}}

plus the medium one but then fails ?
For some reason it really seems to block on this image.

What could go wrong sometimes and not others (?!) in this:

{{- $img := "" -}}
{{- $small := "" -}}
{{- $medium := "" -}}
{{- $large := "" -}}
{{- $largest := "" -}}
 
{{ if hasPrefix .Destination "http" }}
	{{ $img = resources.GetRemote .Destination }}
{{ else }}
	{{ $img = resources.Get .Destination }}
{{end}}
{{- $img_reduced_quality := $img.Resize (print $img.Width "x q90") -}}
{{ if ge $img.Width 1500 }}
	{{ $largest = $img.Resize "1500x q90" }}
	{{ $large = $img.Resize "1200x q90" }}
	{{ $small = $img.Resize "604x q90" }}
{{ else if ge $img.Width 1200 }}
	{{ $largest = $img_reduced_quality }}
	{{ $large = $img.Resize "1200x q90" }}
	{{ $medium = $img.Resize "900x q90" }}
	{{ $small = $img.Resize "604x q90" }}
{{ else if ge $img.Width 900 }}
	{{ $largest = $img_reduced_quality }}
	{{ $large = $img_reduced_quality }}
	{{ $medium = $img.Resize "900x q90" }}
	{{ $small = $img.Resize "604x q90" }}
{{ else if ge $img.Width 600 }}
	{{ $largest = $img_reduced_quality }}
	{{ $large = $img_reduced_quality }}
	{{ $medium = $img_reduced_quality }}
	{{ $small = $img.Resize "604x q90" }}
{{ else }}
	{{ $largest = $img_reduced_quality }}
	{{ $large = $img_reduced_quality }}
	{{ $medium = $img_reduced_quality }}
	{{ $small = $img_reduced_quality }}
{{end}}

{{if .IsBlock}}
<figure {{with .Attributes.id}}id={{.}} {{end}}class="image flex flex-col right {{with .Attributes.class}}{{.}}{{end}}">{{ with .Title }}<figcaption
		class=figcaption_class>{{.|$.Page.RenderString}}</figcaption>{{ end }}
		<a href="{{$img.RelPermalink}}"
		target="_blank">
		<picture>
			<source srcset="
	{{ $small.RelPermalink }} 604w,
	{{ $medium.RelPermalink }} 900w,
	{{ $large.RelPermalink }} 1200w,
	{{ $largest.RelPermalink }} 1500w"
			sizes=
	"(max-width: 604px) 604px,
	(max-width: 1200px) 900px,
	(max-width: 1600px) 1200px,
	1500px" />
				<img src="{{ $medium.RelPermalink }}" width={{ $medium.Width }} height={{ $medium.Height }} loading="{{default "lazy" .Attributes.loading}}" alt="{{ default (.Title|markdownify) .Text }}" {{ with .Title }}title="{{ markdownify . }}" {{end }} />
		</picture>
	</a></figure>
{{ else }}
<span {{with .Attributes.id}}id={{.}} {{end}}class="image flex flex-col right {{with .Attributes.class}}{{.}}{{end}}"> {{ with .Title }}
<span class=figcaption_class>{{.|$.Page.RenderString}}</span>{{ end }}
<a href="{{$img.RelPermalink}}" target="_blank">
		<picture>
			<source srcset="
	{{ $small.RelPermalink }} 604w,
				{{ warnf "%v" $img}}
	{{ $medium.RelPermalink }} 900w,
	{{ $large.RelPermalink }} 1200w,
	{{ $largest.RelPermalink }} 1500w"
			sizes=
	"(max-width: 604px) 604px,
	(max-width: 1200px) 900px,
	(max-width: 1600px) 1200px,
	1500px" />
			<img src="{{ $medium.RelPermalink }}" width={{ $medium.Width }} height={{ $medium.Height }} loading="{{if .Attributes.loading}}{{.}}{{else}}lazy{{end}}" alt="{{ default (.Title|markdownify) .Text }}" {{ with .Title }}title="{{ markdownify . }}" {{end }} />
		</picture>
	</a></span>
{{end}}

You didn’t include a resize to medium when the image is ge 1500 px wide.

...
{{ if ge $img.Width 1500 }}
	{{ $largest = $img.Resize "1500x q90" }}
	{{ $large = $img.Resize "1200x q90" }}
	{{ $medium = $img.Resize "900x q90" }} <-- This is missing from your code
	{{ $small = $img.Resize "604x q90" }}
{{ else if ge $img.Width 1200 }}
...

I corrected and simplified, but it’s not doing anything.

Error: Error building site: “/home/drm/website/content/docs/Love/inceste_en.md:1:1”: “/home/drm/website/themes/hugo-book/layouts/_default/_markup/render-image.html:13:32”: execute of template failed: template: _default/_markup/render-image.html:13:32: executing “_default/_markup/render-image.html” at <$img.Resize>: nil pointer evaluating resource.Resource.Resize

{{- $img := "" -}}
{{- $small := "" -}}
{{- $medium := "" -}}
{{- $large := "" -}}
{{- $largest := "" -}}
 
{{ if hasPrefix .Destination "http" }}
	{{ $img = resources.GetRemote .Destination }}
{{ else }}
	{{ $img = resources.Get .Destination }}
{{end}}
{{ warnf "ici: %v" .Destination }}
{{- $img_reduced_quality := $img.Resize (print $img.Width "x q90") -}}
{{ if gt $img.Width 1500 }}
	{{ $largest = $img_reduced_quality.Resize "1500x" }}
{{ else }}
	{{ $largest = $img_reduced_quality }}
{{ end}}
{{ if gt $img.Width 1200 }}
	{{ $large = $img_reduced_quality.Resize "1200x" }}
{{ else }}
	{{ $large = $img_reduced_quality }}
{{ end}}
{{ if gt $img.Width 900 }}
	{{ $medium = $img_reduced_quality.Resize "900x" }}
{{ else }}
	{{ $medium = $img_reduced_quality }}
{{ end}}
{{ if gt $img.Width 604 }}
	{{ $small = $img_reduced_quality.Resize "604x" }}
{{ else }}
	{{ $small = $img_reduced_quality }}
{{ end}}

I sent you the file that making trouble.

That’s another error. Probably because the resource you’re trying to use does not exist. Make sure that $img is ok before using it.

The warning line tells me which files was worked on the last, and it’s fine, I checked.

The Resize is called on a nil pointer. So something is not “fine” – if it were, you wouldn’t have a nil there.

It just isn’t finding the image.
Sorry, I’d written this before the other posts and got a phone call!

I reduced everything to one file with one image, that produces the same message.
https://drive.google.com/drive/folders/1y-b9KQWT0rn2eY2zOmfXUKO3c08rYQZ1?usp=share_link

in the sample I reduced the whole render hook to just:

{{- $img := "" -}}
{{ if hasPrefix .Destination "http" }}
	{{ $img = resources.GetRemote .Destination }}
{{ else }}
	{{ $img = resources.Get .Destination }}
{{end}}
<img src="{{ $img.RelPermalink }}" width={{ $img.Width }} height={{ $img.Height }} loading="{{if .Attributes.loading}}{{.}}{{else}}lazy{{end}}" alt="{{ default (.Title|markdownify) .Text }}" {{ with .Title }}title="{{ markdownify . }}" {{end }} />

which induces:

Error: Error building site: “/home/drm/sample/content/_index.md:1:1”: “/home/drm/sample/themes/hugo-book/layouts/_default/_markup/render-image.html:7:7”: execute of template failed: template: _default/_markup/render-image.html:7:7: executing “_default/_markup/render-image.html” at <$img.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink
Built in 68 ms

There is ONLY one image, and it’s not the same as before.

I can’t get the google drive link to work, did you remove it?
Where is the image located? It must be in assets, right?

yes, in assets. and static too. But I won’t change it again, because doing so, then going back to before, remove the bug. Yes, hardly reproducible ! Almost parapsychology at this point !
https://drive.google.com/drive/folders/1y-b9KQWT0rn2eY2zOmfXUKO3c08rYQZ1?usp=share_link
I doubt you’ll see the bug.

On your project site (not a test site), please temporarily replace your image render hook with this.

layouts/_default/_markup/render-image.html
{{- /* Determine content path for error messages. */}}
{{- $path := "" }}
{{- with .Page.File }}
  {{- $path = .Path }}
{{- else }}
  {{- $path = .Path }}
{{- end }}

{{- /* Get image resource. */}}
{{- $u := urls.Parse .Destination }}
{{- $r := "" }}
{{- if $u.IsAbs }}
  {{- with resources.GetRemote $u.String }}
    {{- with .Err }}
      {{- errorf "Unable to get remote image %s: %s. See %s" $u.String . $path }}
    {{- else }}
      {{- /* Image is a global resource (remote). */}}
      {{- $r = . }}
    {{- end }}
  {{- else }}
    {{- errorf "Unable to get remote image %s. See %s" $u.String $path }}
  {{- end }}
{{- else }}
  {{- with .Page.Resources.Get $u.Path }}
    {{- /* Image is a page resource. */}}
    {{- $r = . }}
  {{- else }}
    {{- with resources.Get $u.Path }}
      {{- /* Image is a global resource (local). */}}
      {{- $r = . }}
    {{- else }}
      {{- errorf "Unable to get image %s. See %s" $u.Path $path }}
    {{- end }}
  {{- end }}
{{- end }}

{{- if $r }}
  {{- /* Set image element attributes. */}}
  {{- $attributes := merge .Attributes (dict
      "alt" .PlainText
      "height" (string $r.Height)
      "src" $r.RelPermalink
      "title" .Title
      "width" (string $r.Width)
    )
  }}

  {{- /* Render image element. */ -}}
  <img
    {{- range $k, $v := $attributes }}
      {{- if $v }}
        {{- printf " %s=%q" $k $v | safeHTMLAttr }}
      {{- end }}
    {{- end -}}
  >
{{- end -}}

1 Like

I’m a bit suspicious of 3500 wide webp image that is 12MB in size, I use 4K jpgs as source images and the max is around half that size and they are more detailed images.
The site did build though.
Personally I’d lowercase your Images folder - Images and lmages look way too similar (the first is with capital i the second with lowercase L)
As well as case issues with some servers.

your code works yes but no image is processed at all ?
I don’t understand that code at all, I’ll have trouble editing it !
Do I start now or am I waiting for more ? Also, an idea what’s wrong with .Resize ?

Individually each image weights as it should, it’s just that I have a bunch of them. Some are enormous, that’s why I process them. But I want people to access the full res ones when clicking on the displayed version so they’re in assets.

The hook I provided is an example of defensive coding. It prevents the nil pointer error that you reported earlier in this thread, and instead tells you if it cannot find a resource.

Since you didn’t get an error, it found a resource.
But if it didn’t display, then perhaps the resource isn’t an image.

Please post the HTML that was generated for that image (I don’t want to see anything else).

1 Like

Thanks for your efforts, it allowed me to find the culprits without modifying and trying to build a million times. I feel like several things stacked up. Subpaar code (I suppose, not proven though !), image folder not totally updated, perhaps a gift from my text editor too.
Now my baby is entirely back :partying_face: :partying_face: :partying_face:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.