How to avoid newlines caused by conditionals?
Asked Answered
F

2

13

Given this Go text/template code:

Let's say:
{{ if eq .Foo "foo" }}
Hello, StackOverflow!
{{ else if eq .Foo "bar" }}
Hello, World!
{{ end }}

We get the following output in case Foo equals "foo":

Let's say:

Hello, StackOverflow!

(followed by a newline)

Is there a way to get rid of the extra newlines?

I would expect that this can be accomplished using the {{- and -}} syntax:

Let's say:
{{- if eq .Foo "foo" }}
Hello, StackOverflow!
{{- else if eq .Foo "bar" }}
Hello, World!
{{- end }}

However, that yields an illegal number syntax: "-" error.

Forgather answered 9/10, 2016 at 20:48 Comment(6)
Are you sure you got the error? I copy-paste the template in playground, it works as expected. Probably you forgot to put space between - and syntax.Transmission
Maybe your go version is too old? As far as I remember this is a new feature.Haemophilic
@SvenWalter you are correct, it was introduced since 1.6Transmission
@Transmission I'll double-check.Forgather
@SvenWalter pebcak -- it was actually Go 1.5 running on that machineForgather
Adding the "-" before the if/end worked for me.Realpolitik
W
21

In your first template, you have a newline after the static text "Let's say:", and the 2nd line contains only the {{if}} action, and it also contains a newline, and its body "Hello, StackOverflow!" starts in the 3rd line. If this is rendered, there will be 2 newlines between the 2 static texts, so you'll see an empty line (as you posted).

You may use {{- if... to get rid of the first newline, so when rendered, only 1 newline gets to the output, resulting in 2 different lines but no newlines between them:

Let's say:
{{- if eq .Foo "foo" }}
Hello, StackOverflow!
{{- else if eq .Foo "bar" }}
Hello, World!
{{- end }}

Output when Foo is "foo":

Let's say:
Hello, StackOverflow!

Output when Foo is "bar":

Let's say:
Hello, World!

Try it on the Go Playground.

Note that this was added in Go 1.6: Template, and is documented at text/template: Text and Spaces.

If you use the - sign at the closing of the actions -}}, you can even remove all the newlines:

Let's say:
{{- if eq .Foo "foo" -}}
Hello, StackOverflow!
{{- else if eq .Foo "bar" -}}
Hello, World!
{{- end -}}

Output when Foo is "foo" and Foo is "bar":

Let's say:Hello, StackOverflow!
Let's say:Hello, World!

Try this one on the Go Playground.

Weisler answered 10/10, 2016 at 8:13 Comment(1)
I'll accept your answer as this is obviously how we expect to solve this since Go 1.6. +1 for also explaining variations. In my case, the problem was an old Go version floating around, which shouldn't normally happen.Forgather
F
1

There is a new line because you're adding a new line after colons (:)

This works https://play.golang.org/p/k4lazGhE-r Note I just start the first if right after the first colons

Files answered 9/10, 2016 at 21:27 Comment(1)
Technically, this solution is correct. However, I'd like to keep the newlines in the template to improve readability.Forgather

© 2022 - 2024 — McMap. All rights reserved.