How can I write an if statement in resources.Get Hugo?

I have the following code:

{{ with resources.Get .Site.Params.image }}
    <meta property="og:image" content="{{ .Permalink }}" />
{{ end }}

I get a image path in the config.toml file. However I now want to be able to overwrite the file by frontmatter data… Something like this (in pseudo code):

{{ if isset .Image }}
{{ $image := .Image }}
{{ else }}
{{ $image := .Site.Params.image }}
{{ end }}
{{ with resources.Get $image }}
...

How can I write this statement? Also if you have good tutorials on these statements and syntax let me know!

:= is used to define a variable which always has block scope, = is used to assign to a defined variable. So after

{{if ...}} {{x := 5}} {{end}}

x will not be known. Check out the default function, or use something like

{{ x := 5}} <!-- DEFINE x and ASSIGN to it -->
{{ with y }}
{{ x = . }} <!-- set x to another value -->
{{ end }}

My best bet is to read code here in the forum. The Hugo documentation is not for the faint of heart, and your question/problem (i.e. the difference between := and =) has been discussed often here.

1 Like

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