Home >Backend Development >Golang >How can I repeat HTML code a specified number of times using Go templates?

How can I repeat HTML code a specified number of times using Go templates?

Linda Hamilton
Linda HamiltonOriginal
2024-12-27 06:06:20918browse

How can I repeat HTML code a specified number of times using Go templates?

Repeating HTML Code with Go Templates

In web development with Go, there are instances where you may need to iterate an HTML line a specified number of times. Let's explore how to achieve this using Go templates.

Using the {{range}} Action

To reiterate a portion of HTML within a Go template, we can utilize the {{range}} action. However, this action requires an iterable object, such as a slice, array, or map.

Passing a Zero-Value Slice

Instead of creating an actual data structure, we can pass a zero-value slice that essentially has no allocated memory. This slice acts as a placeholder for the {{range}} action to iterate over. For instance:

const templ = `<ul>
{{range $idx, $e := .}}
    <li><a href="/?page={{$idx}}">{{$idx}}</a></li>
{{end}}
</ul>`
tmpl := template.Must(template.New("").Parse(templ))
n := 5
if err := tmpl.Execute(os.Stdout, make([]struct{}, n)); err != nil {
    panic(err)
}

Output:

<ul>

    <li><a href="/?page=0">0</a></li>

    <li><a href="/?page=1">1</a></li>

    <li><a href="/?page=2">2</a></li>

    <li><a href="/?page=3">3</a></li>

    <li><a href="/?page=4">4</a></li>

</ul>

Using a Filled Slice

If you need to start the indices at a value other than zero, consider filling the slice with the desired values and then iterating over it.

tmpl := template.Must(template.New("").Parse(templ))
n := 5
values := make([]int, n)
for i := range values {
    values[i] = (i + 1) * 2
}
if err := tmpl.Execute(os.Stdout, values); err != nil {
    panic(err)
}

Output:

<ul>

    <li><a href="/?page=2">2</a></li>

    <li><a href="/?page=4">4</a></li>

    <li><a href="/?page=6">6</a></li>

    <li><a href="/?page=8">8</a></li>

    <li><a href="/?page=10">10</a></li>

</ul>

Using a Zero-Value Slice and Custom Function

An alternative approach is to register a custom function that increments a given number and returns the result. This allows you to use a zero-value slice and call the function within the template to obtain the desired numbers.

func main() {
    tmpl := template.Must(template.New("").Funcs(template.FuncMap{
        "Add": func(i int) int { return i + 1 },
    }).Parse(templ))
    n := 5
    if err := tmpl.Execute(os.Stdout, make([]struct{}, n)); err != nil {
        panic(err)
    }
}

const templ = `<ul>
{{range $idx, $e := .}}{{$idx := (Add $idx)}}
    <li><a href="/?page={{$idx}}">{{$idx}}</a></li>
{{end}}
</ul>`

Output:

<ul>

    <li><a href="/?page=1">1</a></li>

    <li><a href="/?page=2">2</a></li>

    <li><a href="/?page=3">3</a></li>

    <li><a href="/?page=4">4</a></li>

    <li><a href="/?page=5">5</a></li>

</ul>

The above is the detailed content of How can I repeat HTML code a specified number of times using Go templates?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn