Is it possible to do multiple assignment of params to variables?

Front Matter

team:
    enable: true
    members:
     - Name: "Alice"
       Role: "Tech Lead"
     - Name: "Bob"
       Role: "Manager"

Is this possible ?

{{ with .Params.team }}
{{ if .enable }}
{{ range $person := .members }}
{{ $name,$role := $person.name,$person.role}}
Person is {{$name}} role is {{$role}}<br>
{{end}}
{{end}}
{{end}}

I get this error.

too many declarations in command

May be you can try to avoid $person by doing {{ range .members }} instead.

In this context you will access directly .name, .role with

Person is {{ .name }} role is {{ .role }}

Not tested.

1 Like

Ya, that will work also. I was just wondering if that was possible but if it’s not I’m going to go with your answer.

Thanks

I prefer @divinerites 's way.

But if you really wanted to reuse variables, you have to declare them outside of the loop with :=. Then when inside the loop, reassign them with =.

Notice the := vs = syntax.

1 Like

Is the question whether parallel assignment is supported?

You can tighten it up a bit.

{{ with and .Params.team.enable .Params.team.members }}
  {{ range . }}
    Person is {{ .Name }} role is {{ .Role }}<br>
  {{ end }}
{{ end }}

The and function…

Returns the boolean AND of its arguments by returning the first empty argument or the last argument, that is, “and x y” behaves as “if x then y else x”. All the arguments are evaluated.

https://golang.org/pkg/text/template/#hdr-Functions

2 Likes

Yes, don’t know or think it’s possible.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.