Understanding Hugo Conditionals

Hey guys!

I’m sure a lot of you are Hugo experts, but I wanted to share this article with you that I wrote that discusses Hugo’s approach to templating and conditionals. When discussing Hugo with non-Hugo devs I frequently hear complaints about how “crazy” the templating system is, when I think it is easy to understand and follows a simple ruleset. I hope this article helps clear some of that up.

Check out out: Demystifying Hugo Conditionals

10 Likes

thanks!

1 Like

Thanks for the article. This adds some clarity to conditional evaluation.

A question outside the scope of the article – I’m trying to understand when to use with vs if. I see them used interchangeably in several cases, yet with doesn’t work in this scenario:

{{ with .Resources.Match "itemName" }}
        <h2>{{ .item-name-title }}</h2>
{{ end }}

will error out, yet

{{ if .Resources.Match "itemName" }}
        <h2>{{ .item-name-title }}</h2>
 {{ end }}

will evaluate successfully. What are the parameters to determine when if should be used instead of with?

@asagray I think this is to do with the scope of dot when using with and if . So using with your code is equivalent to

<h2>{{ .Resources.Match.item-name-title</h2>

or maybe

<h2> {{ .itemName.item-name-title }} </h2>
1 Like