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

Time:08-27

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!

CodePudding user response:

This is my solution:

{{ $ogImage := "" }}
{{ if .Params.thumbnailImage }}
    {{ $ogImage = .Params.thumbnailImage }}
{{ else }}
    {{ $ogImage = .Site.Params.ogImage }}
{{ end }}
{{ $ogImageRender := resources.Get $ogImage }}
{{ with $ogImageRender }}
    <meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
  • := defines variables and set initial value
  • = set variables to another value

CodePudding user response:

Use the or function:

{{ with resources.Get (or .Params.thumbnailImage .Site.Params.ogImage) }}
    <meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
  • Related