template with multiple arguments #1176
-
I found that we can have custom templates: https://docs.gomplate.ca/functions/tmpl/ Can we provide multiple arguments to a template? {{ define "Default" -}}
{{ if has .0 .1 }}{{ .0..1 }}{{ else }}{{ .2 }}{{ end }}
{{ end }}
{{ $ds := datasource "ds" }}
foo:
bar: {{ template "Default" $ds keyToCheck defaultValue }} In the example |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
(I've converted this to a discussion as this is more of a question than an issue) The context must be a single item, however that item can be a list (slice) or a map (dict). You can construct your own inline with the {{ define "foo" }}
{{ if has .ds .key }}{{ index .ds .key }}{{ else }}{{ .default }}{{ end }}
{{ end }}
{{ $ds := datasource "ds" }}
foo:
bar: {{ template "foo" dict "ds" $ds "key" $keyToCheck "default" $defaultValue }} (See the docs for more about why For more complex slices and maps it can be awkward to use the {{ $ipRanges := `
internal:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/24
external:
- 1.0.0.0/8
- 123.234.5.0/24
` | yaml }}
{{ template "someTemplate" $ipRanges }} |
Beta Was this translation helpful? Give feedback.
(I've converted this to a discussion as this is more of a question than an issue)
The context must be a single item, however that item can be a list (slice) or a map (dict). You can construct your own inline with the
slice
anddict
functions:(See the docs for more about why
index
is necessary)For more complex slices and maps it can be awkward to use the
slice
anddict
functions extensively, so one pattern I've used a few times is to convert from inline YAML or JSON wi…