douting1871 2017-09-08 09:05
浏览 55
已采纳

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>

How can I implement this?

  • 写回答

1条回答 默认 最新

  • doufeng1249 2017-09-08 09:12
    关注

    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.

    Passing a zero-value slice

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

    The template code:

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

    Testing it:

    tmpl := template.Must(template.New("").Parse(templ))
    n := 5
    if err := tmpl.Execute(os.Stdout, make([]struct{}, n)); err != nil {
        panic(err)
    }
    

    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>
    

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

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

    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>
    

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

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示