Any pointers to when IsSet and With can be dispensed with?

As a newbie I’ve been using a lot of isset and with in my code as in

  {{- with .Params.schema }}
<main itemscope itemtype="https://schema.org/{{ .at_type }}">
   <h2>Where: <span itemprop="name">{{ .name }}</span></h2>
  {{ partial "address.html" .address }}
...

and then the partial looks like

{{- $address := slice .streetaddress }}

<address itemprop="address" itemscope itemtype="https://schema.org/PostalAddress"> &#9993; 
  <span itemprop="streetAddress">{{ .streetaddress }}</span>
{{- if and (isset . "addresslocality") (not (strings.Contains  .streetaddress .addresslocality)) }}
, <span itemprop="addressLocality">{{ .addressLocality }}</span>
{{- end }}
{{- if and (isset . "addressregion") (not (strings.Contains  .streetaddress .addressregion)) }}
, <span itemprop="addressRegion">{{ .addressregion }}</span>
{{- end }}
{{- if and (isset . "postalcode") (not (strings.Contains  .streetaddress .postalcode)) }}
, <span itemprop="postalCode">{{ .postalcode }}</span>
{{- end }}
{{- if isset . "addressCountry" }}
    <meta itemprop="addressCountry" content="{{ .addressCountry }}" >
{{- end }}
</address>

Through trial and error, I’ve discovered all those issets are completely superfluous, and the if statements can often be discarded entirely as in the last line:

{{- $address := slice .streetaddress }}

<address itemprop="address" itemscope itemtype="https://schema.org/PostalAddress"> &#9993; 
  <span itemprop="streetAddress">{{ .streetaddress }}</span>
{{- if and .addresslocality (not (strings.Contains  .streetaddress .addresslocality)) }}
, <span itemprop="addressLocality">{{ .addressLocality }}</span>
{{- end }}
{{- if and .addressregion (not (strings.Contains  .streetaddress .addressregion)) }}
, <span itemprop="addressRegion">{{ .addressregion }}</span>
{{- end }}
{{- if and .postalcode (not (strings.Contains  .streetaddress .postalcode)) }}
, <span itemprop="postalCode">{{ .postalcode }}</span>
{{- end }}
  <meta itemprop="addressCountry" content="{{ .addressCountry }}" >
</address>

One of the reasons for getting rid of isset is I’m getting the data from a huge variet of sites, many of which don’t include address data, leading to lot of warnings when the site gets built.

Since the if…isset statements seem redundant, I’m thinking of removing them all, and simplifying my code to simpl ask for .Page.Params.schema.address.addressCode etc which automagically won’t get rendered if it isn’t set.

I’m rereading the documentation, but haven’t found the rules for when html elements will simply be skipped because the data Hugo has been asked to be insert isn’t available.

One of the things I’m confused about if something I don’t want rendered is nested within a div or whatever, will the entire HTML element be ignored, or just the element with the missing data?

There are only a few edge cases where isset is of any value.

The if, with, and range statements skip the block if the expression if falsy.

That never happens.

Why does just

<meta itemprop="addressCountry" content="{{ .addressCountry }}" >

behave the same as

{{- if isset . "addressCountry" }}
    <meta itemprop="addressCountry" content="{{ .addressCountry }}" >
{{- end }}

ie if .addressCountry isn’t set, Hugo appears to leave the entire <meta ....> line out of the generated HTML.

Post a link to a repo showing this behavior.

Oops, looking at the code generated I see I’m getting

<meta itemprop="addressCountry" content="" >

I thought I’d read somewhere that Hugo skips elements with unset variables, but I must have been mistaken.

Hugo can’t do that, as you (or your template) write the HTML elements. Hugo doesn’t “understand” what you do there, So why would it remove an element that it already had partly written to output, only because a parameter it refers to is empty? That could well be intentional, couldn’t it?

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