html – Go templates iterate over multiple arrays together-ThrowExceptions

Exception or error:

I’ve read through a lot of posts here related to Go templates but I can’t find what I’m looking for.

I have these 2 structs, which I cannot change as they are used elsewhere in this project I am helping with:

type Schedule struct {
    Description   string
    ControlNights int
    PlayNights    int
    StartDay      int
    Combos        []Combo
}

type Combo struct {
    From    time.Time
    Every   int
    Until   time.Time
    Sounds  []string
    Volumes []int
    Waits   []int
}

And I need to display this data on an html page. So I need to iterate over each Combo. Which I can do, but I need to have the Sounds, Volumes and Waits on rows together i.e. Sound x volume y wait z. Where x,y and z are the values in the Sounds, Volumes and Waits arrays. These arrays can be of any length >= 1 but they will all have the same length.

I want it to look like this:

enter image description here

I have tried this:

            {{range .Schedule.Combos}}
            <div class="container">
                <div class="row">
                    <div class="col">
                        <div class="card mt-2 ml-2">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <strong>Timing</strong>
                                </h5>
                                <h5 class="card-text">
                                    Play every {{.Every}} mins
                                </h5>
                                <h5 class="card-text">
                                    From time {{TimeOfDayToString .From}}
                                </h5>
                                <h5 class="card-text">
                                    Until {{TimeOfDayToString .Until}}
                                </h5>
                            </div>
                        </div>
                    </div>
                    <div class="col">
                        <div class="card mt-2">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <strong>Sounds</strong>
                                </h5>
                                {{range .Sounds}}
                                    <h5 class="card-text">
                                        Sound {{.}}
                                    </h5>
                                {{end}}
                            </div>
                        </div>
                    </div>

And that allows me to range over the sounds, but I need to range over the volumes and waits (times to wait until playing next sound) as well, at the same time somehow…

I guess what I could do is make a different Combo struct and pass that to my html template, but I was hoping to avoid that if there’s a way to do it with the structs as they are.

How to solve:

If you are sure that those three arrays have the same number of elements, then you can do this:

{{ $volumes := .Volumes }}
{{ $waits := .Waits }}
{{range $index,$sound := .Sounds }}

Print Sounds[i] like this: {{$sound}}

Print volumes[i] like this: {{index $volumes $index}}

Print waits[i] like this: {{index $waits $index}}

{{end}}

This should give you the idea.

Leave a Reply

Your email address will not be published. Required fields are marked *