Skip to main content

Working with Template Functions

1. Accessing Values ({{ .Values.key }})

Expression Description Example
.Values.key Access a value from the values file {{ .Values.image.repository }}
.Values.key.subkey Access nested values {{ .Values.service.port }}
.Values.key.index Access array elements by index {{ .Values.ports[0] }}

2. Using Pipeline Operations

Operation Description Example
. | quote Quote a value for use in shell commands {{ .Values.name | quote }}
. | upper Convert to uppercase {{ .Values.name | upper }}
. | lower Convert to lowercase {{ .Values.name | lower }}
. | splitList Split a string into a list {{ .Values.tags | splitList "," }}

3. Quoting Strings

Example: Quoting a value for shell usage

helm install my-app ./my-chart --set name={{ .Values.name | quote }}

4. Converting to YAML

Function Description Example
toYaml Converts a value to YAML format {{ .Values.config | toYaml }}
toJson Converts a value to JSON format {{ .Values.config | toJson }}

5. Converting to JSON

Example: Using toYaml and toJson

config:
  database:
    host: {{ .Values.database.host }}
    port: {{ .Values.database.port }}

6. Using Default Values

Function Description Example
default Provide a default value if the first is not set {{ default "default-value" .Values.key }}
empty Check if a value is empty {{ if (empty .Values.key) }}default{{ end }}

7. String Manipulation

Function Description Example
upper Convert string to uppercase {{ .Values.name | upper }}
lower Convert string to lowercase {{ .Values.name | lower }}
trim Remove leading and trailing whitespace {{ .Values.name | trim }}
splitList Split a string into a list using delimiter {{ .Values.tags | splitList "," }}

8. Type Conversion

Function Description Example
atoi Convert string to integer {{ .Values.port | atoi }}
float64 Convert to float64 {{ .Values.memory | float64 }}
toString Convert to string {{ .Values.port | toString }}

9. Encoding Functions

Function Description Example
base64enc Encode a string in base64 {{ .Values.secret | base64enc }}
base64dec Decode a base64 string {{ .Values.secret | base64dec }}
b64enc Alias for base64enc {{ .Values.secret | b64enc }}
b64dec Alias for base64dec {{ .Values.secret | b64dec }}

10. Date Functions

Function Description Example
date Format a date {{ now | date "2006-01-02" }}
dateInZone Format a date in a specific timezone {{ now | dateInZone "2006-01-02" "UTC" }}
unixEpoch Get Unix timestamp {{ now | unixEpoch }}

11. UUID Generation

Example: Generating a UUID

uuid: {{ uuid }}

12. Using fail Function

Example: Failing if a condition is not met

{{ if not .Values.database.host }}{{ fail "Database host must be set" }}{{ end }}

Was this article helpful?