英文:
With Golang Templates how can I set a variable in each template?
问题
如何在每个模板中设置一个变量,以便在其他模板中使用,例如:
在一个模板中设置变量:
{{ set title "Title" }}
然后在我的布局中:
<title>{{ title }}</title>
当渲染时:
tmpl, _ := template.ParseFiles("layout.html", "home.html")
它将根据在home.html
中设置的内容来设置标题,而不需要为每个视图页面创建一个struct
,这并不是必需的。希望我表达清楚,谢谢。
只是为了澄清:
layout.html:
home.html:
{{ set Title "Home" . }}
{{ Title }} Page
英文:
How can I set a variable in each template that I can use in other templates e.g.
{{ set title "Title" }}
in one template then in my layout
<title> {{ title }} </title>
Then when it's rendered
tmpl, _ := template.ParseFiles("layout.html", "home.html")
it will set the title according to whatever was set in home.html
instead of having to make a struct
for each view page when it isn't really necessary. I hope I made sense, thanks.
Just for clarification:
layout.html:
<!DOCTYPE html>
<html>
<head>
<title>{{ title }} </title>
</head>
<body>
</body>
</html>
home.html:
{{ set Title "Home" . }}
<h1> {{ Title }} Page </h1>
答案1
得分: 8
如果你想在另一个模板中使用该值,可以将其传递给点操作符:
{{with $title := "SomeTitle"}}
{{$title}} <-- 在页面上打印该值
{{template "body" .}}
{{end}}
body模板:
{{define "body"}}
<h1>{{.}}</h1> <-- 再次打印 "SomeTitle"
{{end}}
据我所知,不可能向上返回到链中的上一级。所以layout.html
在home.html
之前被渲染,所以你不能传递一个值回去。
在你的例子中,最好的解决方案是使用一个结构体,并使用dot
将其从layout.html
传递到home.html
:
main.go
package main
import (
"html/template"
"net/http"
)
type WebData struct {
Title string
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("layout.html", "home.html")
wd := WebData{
Title: "Home",
}
tmpl.Execute(w, &wd)
}
func pageHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("layout.html", "page.html")
wd := WebData{
Title: "Page",
}
tmpl.Execute(w, &wd)
}
func main() {
http.HandleFunc("/home", homeHandler)
http.HandleFunc("/page", pageHandler)
http.ListenAndServe(":8080", nil)
}
layout.html
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}} </title>
</head>
<body>
{{template "body" .}}
</body>
</html>
home.html
{{define "body"}}
<h1>home.html {{.Title}}</h1>
{{end}}
page.html
{{define "body"}}
<h1>page.html {{.Title}}</h1>
{{end}}
此外,Go语言有一个很好的关于如何使用模板的文档:
http://golang.org/pkg/text/template/
英文:
If you want to use the Value in another template you can pipeline it to the dot:
{{with $title := "SomeTitle"}}
{{$title}} <--prints the value on the page
{{template "body" .}}
{{end}}
body template:
{{define "body"}}
<h1>{{.}}</h1> <--prints "SomeTitle" again
{{end}}
As far as i know it is not possible to go upwards in the chain.
So layout.html
gets rendered before home.html
, so you cant pass a value back.
In your example it would be the best solution to use a struct and pass it from the layout.html
to the home.html
using the dot
:
main.go
package main
import (
"html/template"
"net/http"
)
type WebData struct {
Title string
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("layout.html", "home.html")
wd := WebData{
Title: "Home",
}
tmpl.Execute(w, &wd)
}
func pageHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("layout.html", "page.html")
wd := WebData{
Title: "Page",
}
tmpl.Execute(w, &wd)
}
func main() {
http.HandleFunc("/home", homeHandler)
http.HandleFunc("/page", pageHandler)
http.ListenAndServe(":8080", nil)
}
layout.html
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}} </title>
</head>
<body>
{{template "body" .}}
</body>
</html>
home.html
{{define "body"}}
<h1>home.html {{.Title}}</h1>
{{end}}
page.html
{{define "body"}}
<h1>page.html {{.Title}}</h1>
{{end}}
Also go has a nice documentation on how to use templates:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论