# Accessing parameters in printf

**URL:** https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933
**Category:** Uncategorized
**Created:** [May 10, 2018, 4:32pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933 "2018-05-10T16:32:44Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 4:32pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/1 "2018-05-10T16:32:44Z")

</div>

Does anyone know why this works:

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

```

however this does not:

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

```

That second line returns this:

```auto
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?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 10, 2018, 4:34pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/2 "2018-05-10T16:34:47Z")

</div>

HAve you tried:

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

```

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

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 4:41pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/3 "2018-05-10T16:41:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [May 10, 2018, 5:36pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/4 "2018-05-10T17:36:01Z")

</div>

> [@bradbice](#):
>
> 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. T

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

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 5:42pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/5 "2018-05-10T17:42:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![it-gro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/it-gro/32/4767_2.png) [@it-gro](https://discourse.gohugo.io/u/it-gro)
#### Post date: [May 10, 2018, 5:51pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/6 "2018-05-10T17:51:01Z")

</div>

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

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

```

this means that

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

like here:

```auto
nickname: 

```

> since $.Params.nickname is being found,

dig it e.g. via:

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 10, 2018, 6:07pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/7 "2018-05-10T18:07:58Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 6:31pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/8 "2018-05-10T18:31:02Z")

</div>

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

content/patterns/button.md

```auto
---
title: Button
nickname: button
---

```

layouts/patterns/single.html

```auto
...
<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

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

```

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

---

<div class="post-metadata">

### Author: ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)
#### Post date: [May 10, 2018, 6:33pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/9 "2018-05-10T18:33:27Z")

</div>

> [@bradbice](#):
>
> layouts/patterns/single.html
> 
> ```auto
> ...
> &lt;h1&gt;{{ .Params.nickname }}&lt;/h1&gt;
> ...
> {{ $a := partial (printf "code-views/%s.html" $.Params.nickname) . }}
> 
> ```

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`?_

---

<div class="post-metadata">

### Author: ![it-gro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/it-gro/32/4767_2.png) [@it-gro](https://discourse.gohugo.io/u/it-gro)
#### Post date: [May 10, 2018, 6:43pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/10 "2018-05-10T18:43:00Z")

</div>

> [@bradbice](#):
>
> I’m not sure why that is.

use that

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

```

and you will see …

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [May 10, 2018, 6:45pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/11 "2018-05-10T18:45:03Z")

</div>

> [@bradbice](#):
>
> {{ $a := (partial (printf “components/%s.html” $.Params.nickname .) }}

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

The above has the parens mixed up.

Should be

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

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 7:37pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/12 "2018-05-10T19:37:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 7:38pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/13 "2018-05-10T19:38:05Z")

</div>

This helps as well, thank you @bep

---

<div class="post-metadata">

### Author: ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)
#### Post date: [May 10, 2018, 7:39pm UTC](https://discourse.gohugo.io/t/accessing-parameters-in-printf/11933/14 "2018-05-10T19:39:33Z")

</div>

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.
