Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a for loop inside a Go template

Tags:

go-templates

I am working in Go, and right now I need to print at least 20 options inside a select, so I need to use some kind of loop that goes from 0 to 20 (to get an index).

How can I use a for loop inside a Go template?

I need to generate the sequence of numbers inside the template. I don't have any array to iterate.

EDIT: I need to get something like this:

<select>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
</select>

So, I need to do in the code something like:

<select>
     {{for i := 1; i < 5; i++}}
        <option value="{{i}}">{{i}}</option>
     {{end}}
</select>

But, this doesn't work.

like image 367
Sredny M Casanova Avatar asked Aug 05 '17 20:08

Sredny M Casanova


2 Answers

You can use range in templates as well. See https://golang.org/pkg/text/template/#hdr-Variables

Easiest option might be to just use a slice containing your options:

func main() {
    const tmpl = `
<select>
{{range $val := .}}
     <option value="{{$val}}">{{$val}}</option>
{{end}}
</select>
`
    t := template.Must(template.New("tmpl").Parse(tmpl))

    t.Execute(os.Stdout, []int{1, 2, 3})
}
like image 144
jayme Avatar answered Sep 17 '22 18:09

jayme


Your best bet is to add an "Iterate" function to your func_map.

template.FuncMap{
        "Iterate": func(count *uint) []uint {
            var i uint
            var Items []uint
            for i = 0; i < (*count); i++ {
                Items = append(Items, i)
            }
            return Items
        },
}

Once you register your function map with text/template, you can iterate like this:

{{- range $val := Iterate 5 }}
  {{ $val }}
{{- end }}

I don't know why this useful function isn't part of the default set of functions text/template provides. Maybe they'll add something similar in the future.

like image 25
Jonathan Mayer Avatar answered Sep 21 '22 18:09

Jonathan Mayer