How to check variable is empty? - Pass Variable on it

Hi guys,

So I am basically playing around with Hugo.
I am trying to check for a variable and then in turn pass the variable output but it doesn’t work.

So basically this is the code

This runs into an error
{{ with .Params.Video}} {{.Title}} {{end}}

But this works
{{ with .Params.Video}} True:Title shows up {{end}}

Is there a way to push out an output inside that? Kindly help.

{{ $title := .Title }}
{{ with .Params.Video }}
  {{ $title }}
{{ end }}

It’s all about context (the dot). See:
https://regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/

This may also be helpful, in particular the descriptions of with and range:
https://golang.org/pkg/text/template/#hdr-Actions

1 Like

Amazing! Thank you. Works like magic

Just curious. HOw do you add an else to the with function?

What’s the syntax on that?

{{ $title := .Title }}
{{ with .Params.Video }}
  {{ $title }}
{{ else }}
  <p>.Params.Video is not set.</p>
{{ end }}
1 Like

Thank you. Amazing!

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