Go Templates Whitespace
=======================

August 31, 2021
https://stefan.vanburen.xyz/til/go-templates-whitespace/


When writing <abbr title="HyperText Markup Language">HTML</abbr> templates in Go, knowing about the [whitespace modifiers](https://pkg.go.dev/text/template#hdr-Text_and_spaces) is crucial for keeping both the templates and the output <abbr title="HyperText Markup Language">HTML</abbr> looking good.

For example, if you had the following template, with `.SomeText` being `"foo"` and `.Something` being `true`:

```go-html-template
<a href="/posts">
  <span>
  {{ if .Something }}
  Text
  {{ else }}
  Image
  {{ end }}
  </span>
  {{ .SomeText }}
</a>
```

The actual output <abbr title="HyperText Markup Language">HTML</abbr> would look something like:

```html
<a href="/posts"><span> Text </span> foo </a>
```

Which isn't what you want, because there's extra spaces --- both around "Text" and around "foo".

Instead, you could do:

```go-html-template
<a href="/posts">
  <span>
  {{ if .Something }}
  {{- Text -}}
  {{ else }}
  {{- Image -}}
  {{ end }}
  </span>
  {{- .SomeText -}}
</a>
```

to get

```html
<a href="/posts"><span>Text</span>foo</a>
```

Which makes both the source code and <abbr title="HyperText Markup Language">HTML</abbr> nice and neat.
