I have the following in my values
test:
- a
- b
- c
In my template, I have the following:
{{- range $.Values.test }}
...
someValue: {{ include "staticString" $ }}
...
{{- end }}
I want the value to be included to be dynamically set based on the value of test in the range function, so the value would be "astaticString", "bstaticString", and "cstaticString" for the template to be included depending on the iteration
Is this possible to do?
I have the following in my values
test:
- a
- b
- c
In my template, I have the following:
{{- range $.Values.test }}
...
someValue: {{ include "staticString" $ }}
...
{{- end }}
I want the value to be included to be dynamically set based on the value of test in the range function, so the value would be "astaticString", "bstaticString", and "cstaticString" for the template to be included depending on the iteration
Is this possible to do?
Share Improve this question edited Apr 1 at 20:34 David Maze 161k46 gold badges249 silver badges289 bronze badges asked Apr 1 at 19:06 Dave LDave L 571 silver badge2 bronze badges 01 Answer
Reset to default 0Under the hood, include
is an ordinary Helm-specific Go-templates extension function; it is not a builtin or special form like template
. That means the template engine evaluates both of its parameters as expressions before calling include
, and you can in fact plug in pretty much whatever you like here.
{{- range .Values.test }}
{{- include (cat . "staticstring") $ -}}
{{- end -}}
{{- define "astaticstring" -}}
a: {{ . }}
{{ end -}}
{{- define "bstaticstring" -}}
beta: {{ . }}
{{ end -}}
{{- define "cstaticstring" -}}
{{- toJson . -}}
{{- end -}}
I'd be careful taking user-specified settings in here, though, since you'll get what looks like a template-author error if the user passes values: [d]
or something else undefined.
(Yes, it's possible to use this to build a template-generating system based on dynamically including templates for various common functionality in various contexts. I'd recommend writing that logic in proper Go instead, both for being a more sensible language and for having a robust testing framework.)