.Param function from inside a renderhook

how do I call the .Param function from inside a render hook ?
It says

/home/drm/WEBSITE/themes/hugo-book/layouts/_default/_markup/render-image.html:28:16": execute of template failed: template: _default/_markup/render-image.html:28:16: executing “_default/_markup/render-image.html” at <$.Param>: can’t evaluate field Param in type goldmark.imageLinkContext

the incriminated code is:

{{ $quality := “100”}}
{{ if gt (len $img.Content) 100000 }}
{{ $quality = $.Param .Imaging.Quality }}
{{end}}

I tried .Param "blabla, $.Param "blabla". Not a lot of people seems to use the .Param function.

in my hook this is a site parameter and it works

site.Param.Imaging.Quality

The hooks have a .Page property, so

.Page.Param.Imaging.Quality

could work.

Yes but I want the function, that automatically resorts to the site parameter if the page’s isn’t defined.

site config.yml contains

params:
  imaging:
    quality: 85

page frontmatter contains

imaging:
  quality: 70

Then this {{ $.Param “imaging.quality” }}
will display 70 (or 85 on pages without the frontmatter)

{{ warnf “%d” ($.Param “imaging.quality”)}}

causes:

/home/drm/WEBSITE/themes/hugo-book/layouts/_default/_markup/render-image.html:28:16": execute of template failed: template: _default/_markup/render-image.html:28:16: executing “_default/_markup/render-image.html” at <$.Param>: can’t evaluate field Param in type goldmark.imageLinkContext
Built in 217 ms

so it doesn’t work here.

What I posted does get the value you wanted, how you use it is something else!
What is the code where you are using it?

I know your code should work, but it doesn’t. This is the very beginning of my image renderhook, in themes/hugo-book/layouts/_default/_markup/render-image.html:

{{- $img := “” -}}
{{- $path := “” }}
{{- with .Page.File }}
{{- $path = .Path }}
{{- else }}
{{- $path = .Path }}
{{- end }}
{{- $destination := urls.Parse .Destination }}
{{- if $destination.IsAbs }}
{{- $img = resources.GetRemote $destination.String }}
{{- else }}
{{- with .Page.Resources.Get $destination.Path }}
{{- $img = . }}
{{- else }}
{{- with resources.Get $destination.Path }}
{{- $img = . }}
{{- end }}
{{- end }}
{{- end }}
{{warnf $.Param “imaging.quality”}}

What does this at <$.Param>: can't evaluate field Param in type goldmark.imageLinkContext mean ?

{{- $src := .Page.Resources.GetMatch (.Get 0) }}
{{ $quality := .Page.Param "imaging.quality" }}
{{ $quality }}
<img src="{{ ($src.Resize (print "984x jpg q" $quality)).Permalink }}">

I tested with this shortcode, notice .Page.Param not $.Param

Why shouldn’t it start with $ ? I thought using .Page would call on the frontmatter parameter only ? If it’s some context/syntax shenaningan, I suggest warmly adding a warning in .Param | Hugo too, because users who want to get things done are likely to find this kind of thing tedious at best.

And it still does not do what it’s supposed to.
This outputs 100 constantly, as if ALL pictures weighted less than 100ko, which is wrong.

{{ $quality := 100}}
{{ if gt (len $img.Content) 100000 }}
{{ $quality := .Page.Param “imaging.quality” }}
{{end}}
{{ warnf “%v : %v” .Destination $quality }}

edit: typo error, but still not working without it

imagining or imaging?

Ok, maybe I screwed up that one and ridiculed myself.
But

{{ $quality := default site.Params.imaging.quality .Page.Params.imaging.quality }}
{{ warnf “%d” $quality }}

still outputs <nil>.

{{- $src := .Page.Resources.GetMatch (.Get 0) }}
{{ $quality := default .Site.Params.imaging.quality .Page.Params.imaging.quality }}
{{ $quality }}
<img src="{{ ($src.Resize (print "984x jpg q" $quality)).Permalink }}">

As a shortcode the above works if the page frontmatter is there like:

imaging:
  quality: 71

with the fallback to:

params:
  imaging:
    quality: 92

Check cases, dots, spelling.
The docs show “.Site.Params”
Site Variables | Hugo (gohugo.io)

config.toml

[params.imaging]
quality = 25

content/posts/post-1.md

+++
title = 'Post 1'
date = 2021-01-01T00:00:00-00:00
draft = false
[imaging]
quality = 50
+++

layouts/_default/_markup/render-image.html

{{ $quality := 100 }}
{{ if gt (len $img.Content) 100000 }}
  {{ $quality = .Page.Param "imaging.quality" }}
{{ end }}
{{ $quality }}

Within the conditional above, you need to assign ( = ) not initialize ( := ).

The documentation for the .Param method on .Page needs some love. It’s on my list.

Yes I changed that, with

I suppose the warnf statement is fine ?
What could be wrong in that :
{{ warnf “%v %q %v” (len $img.Content) .Destination $quality}}

to make sure the images themselves always come out right, and they do, <nil> or not.

Then something else is wrong. I don’t post untested code.

And that doesn’t make sense. You have two tokens and one value.

{{ warnf "%v" $quality}}

or

{{ warnf "%q" $quality}}

but not both.

WARN 2023/01/10 13:45:59 45474 “/Images/images_article_instincto/chimp_eating_monkey.webp” 100
WARN 2023/01/10 13:45:59 549658 “/Images/images_article_instincto/singe_rat.webp”

so {{ $quality = .Page.Param "imaging.quality" }} only ever produces <nil> here

config.tml

[imaging]
quality = 75
resampleFilter = ‘lanczos’

Copy and paste the [params] section from your site configuration.

Copy and paste the front matter from the page that is giving you problems.

The only way you get <nil> from the construct above is if the image size is larger than 100000, and .Page.Param can’t find the value in either front matter or in the site config.

This is wrong. See my example.