31

I've seen many examples of dash being use for if statements ("{{- if.."), e.g:

{{- if hasKey .Values.mymap "mykey" }}
    # do something conditional here...
{{- end }}

what is the purpose of the dash in that statement?

amit
  • 2,171
  • 4
  • 31
  • 50

1 Answers1

42

Dash removes the spaces from the output on the side it appears in the template:

https://golang.org/pkg/text/template/#hdr-Text_and_spaces

{{- if ...}}

The above will remove all spaces that appear before the if statement, so if the result of if prints something, it'll be right after the last piece of text without any spaces.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • in that case, what is the point behind the dash in {{- end }}? – amit Jan 06 '20 at 06:45
  • 3
    Removes all spaces after the output of the if body, so the output after {{-end}} now immediately follows the last printed non-space character in the if-body. – Burak Serdar Jan 06 '20 at 06:55
  • 2
    what if move `-` before the closing bracket? does it make any difference? – Lei Yang Sep 08 '20 at 09:25
  • @LeiYang, That will remove all spaces after the closing bracket. – Burak Serdar Sep 08 '20 at 14:51
  • then what's the difference between right dash in first line, and left dash in second line? – Lei Yang Sep 08 '20 at 15:18
  • @LeiYang I don't understand the question, but if you look at the link in the answer, it should make things clear. – Burak Serdar Sep 08 '20 at 15:23
  • 1
    i cannot open that link(in china). i mean, `{{ end -}} \n {{ end }} ` vs `{{ end }} \n {{- end }} ` is there any difference, and which is preferable? – Lei Yang Sep 09 '20 at 00:53
  • Newline is a space character so both will yield the same result. If it was a different character, the first one will remove the spaces before that character, the second one will remove spaces after. – Burak Serdar Sep 09 '20 at 01:28