Interesting unexpected with behaviour

While working with the with command and these two different frontmatter examples :

background:
  color:

background:

{{with .Params.background.color }}
  {{.}}
{{end}}

will fail with the second case while the following will not.

{{with .Params.background}}
  {{with .color }}
    {{.}}
  {{end }}
{{end}}

Why is this important? Our CMS doesn’t add the frontmatter color: if it doesn’t exist. Maybe it will save someone some time.

I also didn’t expect this behaviour given with supposedly checks for existence.

Your 2 examples isn’t functionally the same (the first will always fail), so I’m not sure what they demonstrate.

The first actually works if the frontmatter for background and color exist.

Actually had an edit to make forgot .Params

1 Like

I will put together a demo but until then please delete this topic. It doesn’t have to do with params it has to do with a map I’m passing in to a partial but it might be the 3rd party mod we are using as well. More investigation required.

I think you can rewrite the above to:

{{with (index .Params "background" "color") }}
{{ end }}

And it would work as expected. Not so pretty, but…

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.