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"> ✉
<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"> ✉
<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?