英文:
How to parse HTML template in GO, Google App Engine
问题
我正在尝试使用Google App Engine中的GO语言制作一个页面,该页面可以使用HTML解析来显示数组的内容。
package hello
import (
"fmt"
"html/template"
"net/http"
)
func init() {
http.HandleFunc("/", root)
}
const TemplateHTML = `
<html>
<body>
<table width="700" border="1" align="center">
<tr>
<td>
{{range}} {{.name}} {{end}} {{range}} {{.Count}} {{end}}
</td>
</tr>
</table>
</body>
</html>
`
func root(w http.ResponseWriter, r *http.Request) {
type variables struct {
Name string
Count int
}
var data = []variables{
{"John", 25},
{"George", 35},
{"NoName", 27},
}
//name := variables{"Somebody", 25}
tmpl, err := template.New("i").Parse(TemplateHTML)
if err != nil {
fmt.Fprint(w, err)
}
err = tmpl.Execute(w, data)
if err != nil {
fmt.Fprint(w, err)
}
}
但是我只得到了一个“内部服务器错误”。
当我使用err = tmpl.Execute(w, name)
(你可以在代码的注释中找到“name”)时,一切正常。
你有任何想法是什么问题吗?我是GO语言的新手。
谢谢!
英文:
I'm trying to make a page in GO with Google App Engine which can show the content of an array with HTML parsing.
package hello
import (
"fmt"
"html/template"
"net/http"
)
func init() {
http.HandleFunc("/", root)
}
const TemplateHTML = `
<html>
<body>
<table width="700" border="1" align="center">
<tr>
<td>
{{range}} {{.name}} {{end}} {{range}} {{.Count}} {{end}}
</td>
</tr>
</table>
</body>
</html>
`
func root(w http.ResponseWriter, r *http.Request) {
type variables struct {
Name string
Count int
}
var data = []variables{
{"John", 25},
{"George", 35},
{"NoName", 27},
}
//name := variables{"Somebody", 25}
tmpl, err := template.New("i").Parse(TemplateHTML)
if err != nil {
fmt.Fprint(w, err)
}
err = tmpl.Execute(w, data)
if err != nil {
fmt.Fprint(w, err)
}
}
But I only get an Internal Server Error.
When I used err = tmpl.Execute(w, name)
(you can find 'name' in the comment in the code) it was okay.
Do you have any idea what can be the problem? I'm new in golang.
Thank you!
答案1
得分: 4
var data = []variables{
variables{"John", 25},
variables{"George", 35},
variables{"NoName", 27},
}
const TemplateHTML = `
{{.Name}} | {{.Count}} |
`
英文:
Someone else will respond with a much more concise/accurate answer, but one simple way you can get your above example to work is to change your data
declaration to:
var data = []variables{
variables{"John", 25},
variables{"George", 35},
variables{"NoName", 27},
}
Here, you are making each element of the data
slice of type variables
, which will match the variables
type in the definition. I believe this was the main issue with your current version - the proper data type was not being supplied to data
. Then, you will just need to adjust your template so that your string looks as follows:
const TemplateHTML = `
<html>
<body>
<table width="700" border="1" align="center">
{{range .}}
<tr>
<td>{{.Name}}</td><td>{{.Count}}</td>
</tr>
{{end}}
</tr>
</table>
</body>
</html>
`
The key changes here were: making .Name
uppercase so it matches the property name and wrapping tr
in the {{range .}}
block so that a new row gets created for each entry (I assumed that's what you were looking for - if not, just make sure that range
encompasses whatever dynamic content you want).
答案2
得分: 1
如果template.Parse
返回一个错误,你就不能使用它返回的其他值。如果模板执行失败,你试图在一个空的tmpl
上调用tmpl.Execute
,这将导致恐慌。
英文:
If template.Parse
returns an error, you can't use the other value it returns. You're trying to call tmpl.Execute
on a nil tmpl
if the template execution failed, which would lead to a panic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论