What is The Purpose Of Dash Inside Curly Braces In Helm Template

I had to spend some time to figure out the purpose of dash inside curly brackets in Helm template. There was a lack of clarity around it & I couldn’t find a proper documentation quickly. Helm Template is based on Golang. Basically it uses Go template internally. If we understand the purpose of dash inside curly braces in Go template, we can apply the same logic for Helm Template.

In Go template, dash inside curly braces is used to remove whitespaces from both the sides where it is used. When dash is used, Go template will eat up the whitespaces till it finds another character in both directions. Remember, newline is also whitespace. So newline characters will also be removed till a non-whitespace character is found. We need to be very careful. Actually I faced a bug specifically due to dash eating up newline.

Let’s take few concrete examples & see how dash within curly braces actually behaves. Suppose {{$test}} evaluates to ok.

Input:
1
{{$test}}
2

Output:
1
ok
2

Input:
1
{{- $test}}
2

Output:
1ok
2

As you can see above, all the whitespaces including the newline was removed in the left hand side where dash was used.

Input:
1
{{$test -}}
2

Output:
1
ok2

All whitespaces including the new line got removed in the right hand side where dash was used.

Input:
1
{{- $test -}}
2

Output:
1ok2

All the whitespaces in the left hand side & the right hand side of “ok” got removed including both newlines.

This is how dash within curly braces behaves in Helm Template. As I mentioned, I encountered a bug due to similar scenario. We used Helm template to populate key value pairs. Basically it is like a configuration file. Key is static. Value is dynamic & Helm Template renders it. A simple template file would look like below:

key1={{- $value1 }}
key2={{- $value2 }}

We used dash at left hand side to remove any unwanted whitespace from the value. Basically it was used for trimming whitespace. What happened was we made value1 empty in the source. Let’s say key2 value was val2. So when Helm Template read the source & replaced the values dynamically, the output became same as below:

key1=key2=val2

As $value1 resolved to empty value, all the whitespaces (including the newline) till key2 was eaten up by the first dash. It broke the format of our config file. So that’s why I want you to be careful. There can be unwanted consequences in few corner scenarios.

Leave a Comment