Undocumented else or weird but happy coincidence

I have an archive shortcode that creates little boxes (or “cards” as Bootstrap 4 likes to call them) via a data file.

The template:

<div class="row">

{{ range $index, $item := sort .Site.Data.archive ".slug" "desc" }}
<div class="{{ with $item.container }}{{.}}{{else}}col-sm-6{{end}}">
	<div class="card">
		<div class="card-header">{{ $item.title }}</div>
		<div class="card-body">
			<p>{{ $item.description }}</p>
			<a href="/archiv/{{ $item.slug }}/" class="btn btn-primary">{{ $item.buttontext }}</a>
	{{ $index2 := add $index 1 }}
{{ end }}


In line 4 the magic happens. If my map has an item “container” then that is inserted, if not then col-sm-6 is inserted.

The documentation is mum about the else and I used it because I was used to from a PHP templating class I am using.

Worth mentioning maybe.

By “undocumented” I mean that if you search for “hugo with function” you end up on and there is no mention of it. It might be mentioned on another weird place but not where you end up searching.

That’s a good point.

In the mean time, Hugo gets all the “Go Templating” features. The with is documented here:

1 Like

This is very cool (at least for me). opens a lot of doors. Maybe ALL these inherited Go functions should get a box with a “further information” in the documentation?

Yes, this probably should be documented.

In addition I noticed that the if function is not documented at all. else if and else are possible.

{{if pipeline}} T1 {{else if pipeline2}} T2 {{else}} T0 {{end}}

It’s difficult to keep on mirroring the Go template documentation in Hugo docs …

If you visit:

the very first note is:

The following is only a primer on Go Templates. For an in-depth look into Go Templates, check the official Go docs.

That’s a good point. I just thought it does not make sense to list (almost all?) Hugo functions—and skip the basic if function.

But with … else is the topic here :wink: