Golang代码重复一个html代码n次 [英] Golang code to repeat an html code n times

查看:17
本文介绍了Golang代码重复一个html代码n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 golang 网络应用程序.因为我需要迭代 HTML 行 n 次.

I am working on golang web app. In that I need to iterate an HTML line n number of times.

func index(w http.ResponseWriter, r *http.Request) {
    tmpl := template.Must(template.ParseFiles("templates/index.html"))
    n := 5
    tmpl.Execute(w, n)
}

<ul>
    <li><a href="/?page=1">1</a></li>
    <li><a href="/?page=2">2</a></li>
                    .
                    .
                    .
    <li><a href="/?page=n">n</a></li>
</ul>

我该如何实施?

推荐答案

要在 Go 模板中重复某些内容,您可以使用 {{range}} 操作.但是 {{range}} 动作期望它可以迭代的东西,例如切片、数组或映射.

To repeat something in Go templates, you may use the {{range}} action. But the {{range}} action expects something it can iterate over, e.g. a slice, array or map.

所以你必须喂它.但是不需要内存的空切片就足够了,例如make([]struct{}, n).

So you have to feed that. But an empty slice which requires no memory is sufficient, e.g. make([]struct{}, n).

模板代码:

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)
}

输出(在 Go Playground 上试试):

Output (try it on the Go Playground):

<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>

使用填充切片

正如我们所见,索引从 0 开始.如果这是一个问题,您可以选择不使用索引,而是用您想要的元素显式填充传递的切片.然后模板将如下所示:

Using a filled slice

As we can see, indices start from 0. If this is an issue, you may opt to not use the index, but fill the passed slice explicitly with the elements you want. Then the template will look like this:

const templ = `<ul>
{{range .}}
    <li><a href="/?page={{.}}">{{.}}</a></li>
{{end}}
</ul>`

一个仅提供以 2 开头的奇数的示例测试代码可能如下所示:

And an example test code that feeds only the odd numbers starting with 2 could look like this:

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)
}

这次输出(在 Go Playground 上试试):

Output this time (try it on the Go Playground):

<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>

使用零值切片和自定义函数

如果您不想费心填充切片并且只需要从 1 开始增加数字,另一种选择是注册一个接收数字的函数,将其加 1 并返回结果.所以你可以继续使用一个零值切片的索引,你可以调用自定义函数来获取一个等于索引+1的数字:

Using a zero-valued slice and custom function

If you don't want to bother filling the slice and you only need increasing numbers starting from 1, another option would be to register a function that receives a number, adds 1 to it and returns the result. So you may continue to use the indices of a zero-valued slice, and you can call the custom function to obtain a number equal to index+1:

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>`

这次输出(在Go Playground上试试):

Output this time (try it on the Go Playground):

<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>

这篇关于Golang代码重复一个html代码n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆