How to use the $.Param method?

I am struggling to use the Param method as outlined in the docs. Everything I try gives me an Unexpected "(" in operand error. For example:

{{ printf $.Param("bgimage") }}
{{ $test := $.Param("bgimage") }}

both give me that error.

I’m basically trying to just get the path to a URL from either my config.toml or post frontmatter but am struggling to do it. The $.Param method looks like it does exactly what I want, but it thwarts me.

Try .Params:

{{ printf $.Params.bgimage }}
{{ $test := $.Params.bgimage }}

That doesn’t seem to be selecting the value only if set. It pulls bgimage from frontmatter, but if I delete bgimage from the frontmatter it doesn’t pull the value from config.toml. Instead it just outputs <nil>.

Try

{{ printf $.Param "bgimage" }}

Oh, and if that work, we’ll have to fix the doc … That is my bad as I added this func.

That doesn’t seem to work either, but I don’t see any errors in the server output as it builds. Maybe I’m misunderstanding something else and using it in the wrong place or something? If I do $.Param.bgimage I see the text output. If I do $.Param "bgimage" the page just seems to fail to render.

You want to use $.Params.bgimage for a page param, and $.Site.Params.bgimage for the main site param from config.toml.

I’d you want to use the site param only when the page param doesn’t exist, do something like this:

{{ with $.Params.bgimage }}{{.}}{{ else }}{{ $.Site.Params.bgimage }}{{ end }}

… He is asking about the .Param method that does exactly this.

I just tested this OK on my blog:

See

And see the footer on

http://bepsays.com/2016/01/23/reactjs-template-methods/

Vs the other pages.

Well, there you go. I didn’t even know about that method!

The template variables page says to use $.Param("foo"). We should fix that.

Hmm… I bet I wasn’t putting the key under the [params] heading in config.toml. I just retried it and got it to work. Thank you!