Accessing parameters in printf

Does anyone know why this works:

{{ $a := (partial (printf "components/%s.html" ($.Params.nickname | default "foo")) .) }}

however this does not:

{{ $a := (partial (printf "components/%s.html" $.Params.nickname .) }}

That second line returns this:

error calling partial: Partial "components/%!s(<nil>).html" not found

The only difference I can see is the presence of a default option, which isn’t even used in the first call. It is finding .Params.nickname just fine from the content front-matter and inserting it. The second version does not find it and returns nil. What am I missing?

HAve you tried:

{{ $a := (partial (printf "components/%s.html" ($.Params.nickname) .) }}

Note the extra brackets. Don’t know if that will work but worth a try.

Thank you, yes i tried that as well (I think your example is missing a closed parentheses?) but I still get the nil error.

I think that you, when you dig deeper, finds that it is used somewhere.

@bep Not sure what you mean, but to clarify what I meant: I am defining a default fallback, but since $.Params.nickname is being found, the default isn’t needed or processed.

{{ $a := (partial (printf "components/%s.html" ($.Params.nickname | default "foo")) .) }}
{{ $b := partial (printf "components/%s.html" ($.Params.nickname | default "foo")) . }}
{{ $c := partial (printf "components/%s.html" $.Params.nickname) . }}
{{/* $d := (partial (printf "components/%s.html" $.Params.nickname .)  */}}
{{ $e := (partial (printf "components/%s.html" $.Params.nickname) .)  }}

a) your working example
b) removed unnecessary ()
c) if you think, you do not need a default
d) your second example - there a ) is missing - so this does not work
e) your second example with ) added

So if you get

error calling partial: Partial "components/%!s(<nil>).html"

this means that

  1. .nickname is not present
    or
  2. .nickname is empty

like here:

nickname: 

since $.Params.nickname is being found,

dig it e.g. via:

{{- with $.Params.nickname }}
{{ $f := partial (printf "components/%s.html" $.Params.nickname) . }}
{{- else}}
{{- errorf "missing nickname at %s:" $.Page.Permalink }}
{{- end}}

f) your code without default - but don’t call partial if nickname is not given. Show then page with the missing nickname.

Maybe you answered your own question - perhaps a missing )? Because I copied your non-working example and added matching brackets to it.

1 Like

Something is missing because this just isn’t working the way it’s supposed to. Here is my exact usage:

content/patterns/button.md

---
title: Button
nickname: button
---

layouts/patterns/single.html

...
<h1>{{ .Params.nickname }}</h1>
...
{{ $a := partial (printf "code-views/%s.html" $.Params.nickname) . }}
{{ highlight $a "html" "" }}
...

The h1 is printing the parameter value of “buttons” just fine. Hugo is still giving me the error of

error calling partial: Partial "code-views/%!s(<nil>).html" not found

for the printf statement. I’m not sure why that is.

You need to ensure that each of your content files has that nickname param set; else you will get that error.

Or just use that default?

1 Like

use that

...
{{- errorf "missing nickname at %s:" $.Page.Permalink }}
...

and you will see …

I think I and some others are missing the real issue here …

The above has the parens mixed up.

Should be

{{ $a := (partial (printf "components/%s.html" $.Params.nickname ) . ) }}

Or something like that. But it would also be good to check that the params existis before using it.

1 Like

That’s it! That is what i was missing. Thank you!

This helps as well, thank you @bep

The problem I was encountering is that I had 2 content files in my site as I am building and testing, and one of the content files did not include the nickname parameter in the front-matter.

Thanks to everyone who contributed suggestions and help.