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

Front Matter

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

Is this possible ?

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

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.


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 }}
  {{ 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.


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.