# .Param function from inside a renderhook

**URL:** <https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317>\
**Category:** support\
**Created:** [January 9, 2023, 1:26am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317 "2023-01-09T01:26:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 9, 2023, 1:26am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/1 "2023-01-09T01:26:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [January 9, 2023, 7:20am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/2 "2023-01-09T07:20:10Z")

</div>

> [@Tom\_Durand](#):
>
> .Param .Imaging.Quality

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.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 9, 2023, 10:05am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/3 "2023-01-09T10:05:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [January 9, 2023, 4:04pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/4 "2023-01-09T16:04:17Z")

</div>

site config.yml contains

```auto
params:
  imaging:
    quality: 85

```

page frontmatter contains

```auto
imaging:
  quality: 70

```

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

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 9, 2023, 4:28pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/5 "2023-01-09T16:28:12Z")

</div>

> {{ 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.

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [January 9, 2023, 4:31pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/6 "2023-01-09T16:31:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 9, 2023, 4:48pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/7 "2023-01-09T16:48:47Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [January 9, 2023, 4:54pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/8 "2023-01-09T16:54:17Z")

</div>

```auto
{{- $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

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 9, 2023, 5:08pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/9 "2023-01-09T17:08:11Z")

</div>

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](https://gohugo.io/functions/param/) **too** , because users who want to get things done are likely to find this kind of thing tedious at best.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 10, 2023, 11:28am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/10 "2023-01-10T11:28:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [January 10, 2023, 11:32am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/11 "2023-01-10T11:32:41Z")

</div>

> [@Tom\_Durand](#):
>
> {{ $quality := default site.Params.imagining.quality .Page.Params.imaging.quality }}

imagining or imaging?

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 10, 2023, 11:38am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/12 "2023-01-10T11:38:27Z")

</div>

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\>.

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [January 10, 2023, 11:58am UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/13 "2023-01-10T11:58:45Z")

</div>

```auto
{{- $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:

```auto
imaging:
  quality: 71

```

with the fallback to:

```auto
params:
  imaging:
    quality: 92

```

Check cases, dots, spelling.  
The docs show “.Site.Params”  
[Site Variables | Hugo (gohugo.io)](https://gohugo.io/variables/site/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2023, 12:29pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/14 "2023-01-10T12:29:47Z")

</div>

config.toml

```plaintext
[params.imaging]
quality = 25

```

content/posts/post-1.md

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

```

layouts/\_default/\_markup/render-image.html

```plaintext
{{ $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.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 10, 2023, 12:39pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/15 "2023-01-10T12:39:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2023, 12:40pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/16 "2023-01-10T12:40:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2023, 12:42pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/17 "2023-01-10T12:42:44Z")

</div>

> [@Tom\_Durand](#):
>
> {{ warnf “%q %v” $quality}}

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

```plaintext
{{ warnf "%v" $quality}}

```

or

```plaintext
{{ warnf "%q" $quality}}

```

but not both.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [January 10, 2023, 12:45pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/18 "2023-01-10T12:45:29Z")

</div>

> 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’

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2023, 12:49pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/19 "2023-01-10T12:49:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2023, 12:49pm UTC](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317/20 "2023-01-10T12:49:56Z")

</div>

> [@Tom\_Durand](#):
>
> [imaging]  
> quality = 75  
> resampleFilter = ‘lanczos’

This is wrong. See my example.

[Next page](https://discourse.gohugo.io/t/param-function-from-inside-a-renderhook/42317.md?page=2)
