英文:
gotemplate not rendering data
问题
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
layout := "static/layout.html"
tmplFiles := []string{layout, tmpl}
fmt.Printf("%v\n", data)
t, err := template.ParseFiles(tmplFiles...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.ExecuteTemplate(w, "layout.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main(){
http.HandleFunc("/directors", directorsHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Println("Server listening on port 8080...")
http.ListenAndServe(":8080", nil)
}
directors.html
{{ define "content" }}
Directors ----
<h1>{{ .Title }}</h1>
<ul>
{{ range .Directors }}
hi
<li>
<p><strong>Name:</strong> {{ .Name }}</p>
<p><strong>Date of Birth:</strong> {{ .DateOfBirth }}</p>
<p><strong>Nationality:</strong> {{ .Nationality }}</p>
</li>
{{ end }}
</ul>
{{ end }}
func directorsHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Title string
Directors []struct {
Name string
DateOfBirth string
Nationality string
}
}{
Title: "Director List",
Directors: []struct {
Name string
DateOfBirth string
Nationality string
}{
{
Name: "Christopher Nolan",
DateOfBirth: "July 30, 1970",
Nationality: "British",
},
{
Name: "Quentin Tarantino",
DateOfBirth: "March 27, 1963",
Nationality: "American",
},
// Add more directors as needed
},
}
renderTemplate(w, "static/directors.html", data)
}
不知道为什么 .Title
没有被渲染出来。
我怀疑是因为接口的原因,但没有找到相关的信息。
我使用匿名结构体将不同的数据发送到不同的页面。
我尝试将信息发送到 directors.html
模板的内容,但它不会被渲染出来。
更多细节:
数据被发送到 data interface{}
。
英文:
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
layout := "static/layout.html"
tmplFiles := []string{layout, tmpl}
fmt.Printf("%v\n", data)
t, err := template.ParseFiles(tmplFiles...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.ExecuteTemplate(w, "layout.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main(){
http.HandleFunc("/directors", directorsHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Println("Server listening on port 8080...")
http.ListenAndServe(":8080", nil)
}
directors.html
{{ define "content" }}
Directors ----
<h1>{{ .Title }}</h1>
<ul>
{{ range .Directors }}
hi
<li>
<p><strong>Name:</strong> {{ .Name }}</p>
<p><strong>Date of Birth:</strong> {{ .DateOfBirth }}</p>
<p><strong>Nationality:</strong> {{ .Nationality }}</p>
</li>
{{ end }}
</ul>
{{ end }}
func directorsHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Title string
Directors []struct {
Name string
DateOfBirth string
Nationality string
}
}{
Title: "Director List",
Directors: []struct {
Name string
DateOfBirth string
Nationality string
}{
{
Name: "Christopher Nolan",
DateOfBirth: "July 30, 1970",
Nationality: "British",
},
{
Name: "Quentin Tarantino",
DateOfBirth: "March 27, 1963",
Nationality: "American",
},
// Add more directors as needed
},
}
renderTemplate(w, "static/directors.html", data)
}
don't know why .Title is not being rendered.
i have the suspicion that its because the interface but didn't find anything about it.
i am using anonymous struct to send different data to different pages.
I tried to send information to directors.html template content but it wont render.
more details:
the data is being send to data interface{} .
答案1
得分: 1
在发送数据到具有布局的页面时发现问题,你需要写成{{template "content" .}}而不是{{ template "content" }}。需要添加一个点。
英文:
Problem found when sending data to a page with layout you need to write {{template "content" .}} instead of {{ template "content" }}.
needed to add a dot.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论