英文:
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}
(未经测试)
无论如何,这是我用新的模板包解决的方案。也许你可以以某种方式使用它
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
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 (
"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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论