Google Go模板用于具有二维数组的Web开发

huangapple go评论107阅读模式
英文:

Google Go Templates for Web Development with two dimensional array

问题

我有一个带有二维数组的结构体示例:

type Foo struct{
testArray[9][9] int
}

我想通过模板来访问它。

例如:
tmpl.Execute(w, foo) // foo是指向Foo结构体的指针,w是解析的HTML网站

我如何在模板中访问数组?对于旧的模板包中的一维数组,代码如下:

{.repeated section testArray}
<p>{@}</p>
{.end}

但是对于二维数组,语法是什么样的?例如,我必须访问testArray[0][0]。

英文:

I have a struct example with a two dimensinal array:

type Foo struct{
testArray[9][9] int
}

I would like to access it by a template.

For example:
tmpl.Execute(w, foo) //foo is a pointer to Foo struct and w is the parsed html site

How can I access the array in the template? For a one dimensional array with the old template package there is the code:

{.repeated section testArray}
<p>{@}</p>
{.end}

but what is the syntax for a two dimension array? For example I must access testArray[0][0]

答案1

得分: 4

我已经意外地用新的模板包解决了你的问题。但是也许旧的模板也可以工作得类似。你尝试过类似这样的东西吗:

{.repeated section testArray}<p>{.repeated section @}{@} {.end}</p>{.end}

(未经测试)

无论如何,这是我用新的模板包解决的方案。也许你可以以某种方式使用它 Google Go模板用于具有二维数组的Web开发

package main

import (
	"os"
	"text/template"
)

type Foo struct {
	Data [9][9]int
}

func main() {
	tmpl := template.Must(template.New("example").Parse(`
	<table>
	{{range .Data}}
	<tr>
	  {{range .}}<td>{{.}}</td>{{end}}
	</tr>
	{{end}}
	`))
	foo := new(Foo)
	foo.Data[2][1] = 4
	tmpl.Execute(os.Stdout, foo)
}
英文:

I have solved your problem accidentally with the new template package. But maybe the old one works similar. Have you tried something like:

{.repeated section testArray}<p>{.repeated section @}{@} {.end}</p>{.end}

(untested)

Anyway, here is my solution with the new template package. Maybe you can use it somehow Google Go模板用于具有二维数组的Web开发

package main

import (
	"os"
	"text/template"
)

type Foo struct {
	Data [9][9]int
}

func main() {
	tmpl := template.Must(template.New("example").Parse(`
	<table>
	{{range .Data}}
	<tr>
	  {{range .}}<td>{{.}}</td>{{end}}
	</tr>
	{{end}}
	`))
	foo := new(Foo)
	foo.Data[2][1] = 4
	tmpl.Execute(os.Stdout, foo)
}

答案2

得分: 2

package main

import (
"net/http"
"text/template"
)

type Foo struct {
Array [9][9]int
}

func handler(w http.ResponseWriter, r *http.Request) {
var foo Foo

for i := 0; i < len(foo.Array); i++ {
    for j := 0; j < len(foo.Array); j++ {
        foo.Array[i][j] = j
    }
}

tmpl := template.Must(template.New("example").Parse(`
    <html>
    <body>
    <table>
        {{ $a := .Array }}
        {{ range $a }}
        <tr>
        {{ $elem := . }}
        {{ range $elem }}
        {{ printf "<td>%d<td>" . }}
        {{ end}}
        </tr>
        {{end}}
    </table>
    </body>
    </html>
`))

tmpl.Execute(w, foo)

}

func main() {
bindAddress := "127.0.0.1:8080"

http.HandleFunc("/", handler)
http.ListenAndServe(bindAddress, nil)

}

英文:

this little sample should help you.

package main

import (
        &quot;net/http&quot;
        &quot;text/template&quot;
)

type Foo struct {
        Array [9][9]int
}

func handler(w http.ResponseWriter, r *http.Request) {
        var foo Foo

        for i := 0; i &lt; len(foo.Array); i++ {
                for j := 0; j &lt; len(foo.Array); j++ {
                        foo.Array[i][j] = j
                }
        }

        tmpl := template.Must(template.New(&quot;example&quot;).Parse(`
                &lt;html&gt;
                &lt;body&gt;
                &lt;table&gt;
                        {{ $a := .Array }}
                        {{ range $a }}
                        &lt;tr&gt;
                        {{ $elem := . }}
                        {{ range $elem }}
                        {{ printf &quot;&lt;td&gt;%d&lt;td&gt;&quot; . }}
                        {{ end}}
                        &lt;/tr&gt;
                        {{end}}
                &lt;/table&gt;
                &lt;/body&gt;
                &lt;/html&gt;
                `))

        tmpl.Execute(w, foo)
}

func main() {
        bindAddress := &quot;127.0.0.1:8080&quot;

        http.HandleFunc(&quot;/&quot;, handler)
        http.ListenAndServe(bindAddress, nil)

}

huangapple
  • 本文由 发表于 2011年11月24日 22:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/8259016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定