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

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

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

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

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

(未经测试)

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

  1. package main
  2. import (
  3. "os"
  4. "text/template"
  5. )
  6. type Foo struct {
  7. Data [9][9]int
  8. }
  9. func main() {
  10. tmpl := template.Must(template.New("example").Parse(`
  11. <table>
  12. {{range .Data}}
  13. <tr>
  14. {{range .}}<td>{{.}}</td>{{end}}
  15. </tr>
  16. {{end}}
  17. `))
  18. foo := new(Foo)
  19. foo.Data[2][1] = 4
  20. tmpl.Execute(os.Stdout, foo)
  21. }
英文:

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

  1. {.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开发

  1. package main
  2. import (
  3. "os"
  4. "text/template"
  5. )
  6. type Foo struct {
  7. Data [9][9]int
  8. }
  9. func main() {
  10. tmpl := template.Must(template.New("example").Parse(`
  11. <table>
  12. {{range .Data}}
  13. <tr>
  14. {{range .}}<td>{{.}}</td>{{end}}
  15. </tr>
  16. {{end}}
  17. `))
  18. foo := new(Foo)
  19. foo.Data[2][1] = 4
  20. tmpl.Execute(os.Stdout, foo)
  21. }

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

  1. for i := 0; i < len(foo.Array); i++ {
  2. for j := 0; j < len(foo.Array); j++ {
  3. foo.Array[i][j] = j
  4. }
  5. }
  6. tmpl := template.Must(template.New("example").Parse(`
  7. <html>
  8. <body>
  9. <table>
  10. {{ $a := .Array }}
  11. {{ range $a }}
  12. <tr>
  13. {{ $elem := . }}
  14. {{ range $elem }}
  15. {{ printf "<td>%d<td>" . }}
  16. {{ end}}
  17. </tr>
  18. {{end}}
  19. </table>
  20. </body>
  21. </html>
  22. `))
  23. tmpl.Execute(w, foo)

}

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

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

}

英文:

this little sample should help you.

  1. package main
  2. import (
  3. &quot;net/http&quot;
  4. &quot;text/template&quot;
  5. )
  6. type Foo struct {
  7. Array [9][9]int
  8. }
  9. func handler(w http.ResponseWriter, r *http.Request) {
  10. var foo Foo
  11. for i := 0; i &lt; len(foo.Array); i++ {
  12. for j := 0; j &lt; len(foo.Array); j++ {
  13. foo.Array[i][j] = j
  14. }
  15. }
  16. tmpl := template.Must(template.New(&quot;example&quot;).Parse(`
  17. &lt;html&gt;
  18. &lt;body&gt;
  19. &lt;table&gt;
  20. {{ $a := .Array }}
  21. {{ range $a }}
  22. &lt;tr&gt;
  23. {{ $elem := . }}
  24. {{ range $elem }}
  25. {{ printf &quot;&lt;td&gt;%d&lt;td&gt;&quot; . }}
  26. {{ end}}
  27. &lt;/tr&gt;
  28. {{end}}
  29. &lt;/table&gt;
  30. &lt;/body&gt;
  31. &lt;/html&gt;
  32. `))
  33. tmpl.Execute(w, foo)
  34. }
  35. func main() {
  36. bindAddress := &quot;127.0.0.1:8080&quot;
  37. http.HandleFunc(&quot;/&quot;, handler)
  38. http.ListenAndServe(bindAddress, nil)
  39. }

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:

确定