简体   繁体   English

在模板中,我如何拥有一个柜台?

[英]In a template, how can I have a counter?

I require basic incrementation arithmetic and loop for my project to keep the template readable and populate a 9x9 grid with values. 我需要为我的项目使用基本的增量算法和循环,以保持模板的可读性,并使用值填充9x9网格。 The values are stored in a string array and so it is necessary to be able to have control over the index. 这些值存储在字符串数组中,因此必须能够控制索引。

Here is my handler with both template functions defined: 这是定义了两个模板函数的处理程序:

func HomeHandler(w http.ResponseWriter, req *http.Request) {
    t := template.New("home.html").Funcs(template.FuncMap{
        "loop": func(n int) []struct{} {
            return make([]struct{}, n)
        },
    }).Funcs(template.FuncMap{
        "inc": func(n int) int {
            return n + 1
        },
    })

    t, err := t.ParseFiles("templates/home.html")

    if err != nil {
        log.Print("template/home error:", err)
    }
    t.ExecuteTemplate(w, "home.html", nil)
}

To create the grid I use the loop function like so: 为了创建网格,我使用如下循环函数:

{{ range loop 3}}
<tbody>
    {{ range loop 3}}
    <tr>
        {{ range loop 9}} 
        <td> <input value="1" type="text" name="value[]" maxlength="1" size="1">
        {{end}}
    {{end}}
{{end}}

However, I'd like to set the value attribute of the input element to the correct value with my data. 但是,我想使用我的数据将输入元素的value属性设置为正确的值。 I believe that I can access the index with the following: 我相信我可以通过以下方式访问索引:

{{index .MyArray 3}}

I will replace "3" with a counter that I'll need to be able to increment correctly. 我将用需要正确递增的计数器替换“ 3”。

Unfortunately, it seems like I can't correctly reassign the variable as I can only increment it from 0 to 1 at most. 不幸的是,似乎我无法正确地重新分配变量,因为我最多只能将其从0递增到1。

Here is my template with my counter: 这是我的柜台模板:

{{$count := 0}}
{{ range loop 3}}
<tbody>
    {{ range loop 3}}
    <tr>
        {{ range loop 9}} 
        {{$count := inc $count}}
        <td> <input value="1" type="text" name="value[]" maxlength="1" size="1">
        {{end}}
    {{end}}
{{end}}

How about doing it the other way around, since ranging over the actual data is something that templates do easily? 由于模板可以轻松地对实际数据进行处理,因此反过来又如何呢? You can do without loop and inc and start with 您可以不使用loopinc开始

{{ range $index, $value := .MyArray }}
<td><input value="{{ $value }}" type="text" name="value[]" maxlength="1" size="1">
{{ end }}

which will get you all of the inputs you need, but without the surrounding markup, so how do we get that? 这将为您提供所需的所有输入,但是没有周围的标记,那么我们如何获得呢? A little bit of modular arithmetic. 一点模块化的算法。 Define a template function like so: 定义一个模板函数,如下所示:

"each": func(interval, n int) bool {
    return n % interval == 0
}

then we can pad things out: 然后我们就可以解决问题:

{{ range $index, $value := .MyArray }}
{{ if each 27 $index }}<tbody>{{ end }}
{{ if each 9 $index }}<tr>{{ end }}
<td><input value="{{ $value }}" type="text" name="value[]" maxlength="1" size="1">
{{ end }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM