0

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?

0

1 Answer 1

0

Under 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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.