I have a function which returns a multiline string. This function is called at severale places - so I don't want to modify it.However at one place I need to prefix each line with a character.
{{- define "myfunc" -}}
aaa
bbb
ccc
ddd
{{- end -}}
At one place in my templates I need to bind these to a list as elements with prefix "-"
expected result:
args:
- aaa
- bbb
- ccc
- ddd
My attempt 1:
args:
- {{- include "myfunc" . | nindent 8 }}
result:
args:
-
aaa
bbb
ccc
ddd
My attempt 2:
iterating over it, something like
args:
{{- range include "myfunc" . }}
- {{ . }}
{{- end }}
result:
Error: template : .... range can't iterate over aaa
bbb
ccc
ddd
My actual solution:
Just copy the existing function with a bit of modification
{{- define "myfunc2" -}}
- aaa
- bbb
- ccc
- ddd
{{- end -}}
The question is how to bind the original function's result to args? How to apply a modification to each line?
You may be able to treat it as a set of string data, and then process it in the same way as a string
_helper.yaml
{{- define "myfunc" -}}
aaa
bbb
ccc
ddd
{{- end -}}
template/xxx.yaml
args:
{{- $data := include "myfunc" . }}
{{- range ( split "\n" $data) }}
- {{ . }}
{{- end }}
output:
args:
- aaa
- bbb
- ccc
- ddd