Best practice for short-circuit if statements?

Often, in my themes, there are optional parameters, etc. In pseudocode, it goes something like this:

if thing has a value
do something with that value
end

The problem I run into is that it’s a twofold operation. I need to check if the value isset, but also that it’s not blank. In “normal” go, a shortcircuit if would do that:

if isset thing && ne thing

The second would never be attempted since the first failed, ya know? But if I just check for “is thing blank”, but the page didn’t have that parameter at all, it bails.

Is there a better way to short-circuit?

Here’s a bit of example code:

{{- if isset .Params "twitter" -}}
      {{- if ne .Params.twitter "" -}}
        <a href = "https://twitter.com/{{ .Params.twitter }}"><i class="fa fa-twitter fa-2x" aria-hidden="true"></i></a>&nbsp;
      {{- end -}}
    {{- end -}}

That’s not super terribad, except that it’s ugly. Where it gets really ugly is when I need to put in an else - I end up repeating code:

          {{- if and (ge (dateFormat "2006-01-02" (dateFormat "2006-01-02" now)) (dateFormat "2006-01-02" $e.registration_date_start)) (le (dateFormat "2006-01-02" (dateFormat "2006-01-02" now)) (dateFormat "2006-01-02" $e.registration_date_end)) }}
            {{ if $e.registration_link }}
              {{ if eq $e.registration_link "" }}
                {{ $.Scratch.Set "registration_link" (printf "/events/%s/propose" $event_slug)}}
              {{ else }}
                {{ $.Scratch.Set "registration_link" $e.registration_link }}
              {{ end }}
            {{ else }}
              {{ $.Scratch.Set "registration_link" (printf "/events/%s/propose" $event_slug)}}
            {{ end }}

Thoughts?

@mattstratton For the first example, I think with will save you some palm-to-face slapping. As for the second example, I’ll take a look at trying to make it DRYer when I get into the office.

https://hugodocs.info/functions/with/

Also on gohugo.io under the Go template primer, I believe, but I’m on my phone and have all the docs concept site URLs essentially memorized at this point…

You’re right. with totally would make that better.