Skip to content

template with multiple arguments #1176

Discussion options

You must be logged in to vote

(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 and dict functions:

{{ 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 index is necessary)

For more complex slices and maps it can be awkward to use the slice and dict functions extensively, so one pattern I've used a few times is to convert from inline YAML or JSON wi…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@daniel-torok
Comment options

Answer selected by daniel-torok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1174 on June 17, 2021 12:50.