What is the difference between helm syntax {{ something }} and {{- something }}?

The Helm template syntax is based on the Go programming language’s text/template package.
The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

The Helm documentation at https://helm.sh/docs/chart_template_guide/control_structures/
discusses why this syntax is needed in an example.

YAML ascribes meaning to whitespace, so managing the whitespace becomes pretty important. […] the curly brace syntax {{ of template declarations can be modified with special characters to tell the template engine to chomp whitespace. {{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed. Be careful! Newlines are whitespace!

So the answer is this. The difference between the {{ syntax and the {{- syntax is that the {{- something }} will result in space on the left being removed. Without this any extra space would be included which could result in incorrectly formatted YAML.

Refer to the Helm documentation which goes into great length about how this syntax works and removes extra spaces.

You’ll frequently see the dash showing up in control structures because without this extra space would be added to your YAML file which could result in invalid syntax being created. So, for example,

{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}

Causes the property apiVersion to be output (in the YAML file) without adding blank lines before and after the property.

Simple example

The Go templating documentation says

when executing the template whose source is

"{{23 -}} < {{- 45}}"

the generated output would be

"23<45"

This shows that the dash syntax causes white space to be removed.

Learning to experiment with Helm syntax

Below I’ll explain how you can begin experimenting with helm syntax
using a simple throw away project.

The commands below I create a temp directory testhelm and go into it, and then run create helm mytest to create a helm application.

Next, I create a sample helm YAML file. This is the file you want to put what you want to test inside of. Below I used the file mytest/templates/my.yaml but any file can be created.

Helm apparently takes all of the files in the templates directory and parses/processes them to create the YAML output (which is used to create a Kubernetes YAML file to configure a K8S application).

In our case, we just leverage the helm command create a test bed for us to play around with.

If you are on a UNIX-based system you should be able to copy and paste the entire code sample below to create the testbed to begin experimenting.

mkdir testhelm
cd testhelm

helm create mytest

cat <<EOF > mytest/templates/my.yaml
expression1: "{{   23    }} < {{     45    }}"
expression2: "{{   23    -}} < {{-    45    }}"

aTest0: ArgWithNoSpace
aTest1:      Arg with spaces on left and right
aTest2:      "    spaces-on-left-and-right    "
aTest3: {{ "     spaces-on-left-and-right   " }}
aTest4: {{ "     spaces-on-left-and-right   " | trim | quote }}

aTest5: Some
{{- "Thing Funky is" -}} goingOn
     {{- "    here"}}
drink2: {{ .Values.drink2 | default "coffee" | quote }}
aTest6: Some    {{ "Thing Funky is"   }}goingOn    {{    "    here"}}
aTest7: Some    {{      "Thing Funky is"   }}goingOn    {{    "    here"}}

EOF

Then run run the helm template command as shown below, and study the output that you get.

helm template myproj ./mychart | less
. . . output trimmed . . .
# Source: mychart/templates/my.yaml
expression1: "23 < 45"
expression2: "23<45"

aTest0: ArgWithNoSpace
aTest1:      Arg with spaces on left and right
aTest2:      "    spaces-on-left-and-right    "
aTest3:      spaces-on-left-and-right
aTest4: "spaces-on-left-and-right"

aTest5: SomeThing Funky isgoingOn    here
drink2: "coffee"
aTest6: Some    Thing Funky isgoingOn        here
aTest7: Some    Thing Funky isgoingOn        here

The first two name/value pairs expression1 and expression2 show the difference with and without dash syntax being used.

Notice also the syntax for aTest5 which resulted in several lines being merged into a single line of output.

Notice also aTest6 and aTest7 look different in the source but produce the same output; spaces within the {{ }} are not output unless they within quotes.

Using this approach digest the Helm syntax in bite sized chunks and so when you need to fix something you can understand what you are seeing.

Leave a Comment