最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Helm template how to include dynamic template - Stack Overflow

programmeradmin6浏览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?

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 0
Add a comment  | 

1 Answer 1

Reset to default 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.)

发布评论

评论列表(0)

  1. 暂无评论