英文:
With ParseGlob how to render more than two templates in golang?
问题
尝试了这个链接 https://github.com/golang-samples/template/blob/master/parseglob/main.go 但是服务器强制关闭了。
-
main.go
package mainimport (
"log"
"os"
"text/template"
)func main() {
t := template.New("main.tmpl")
t = template.Must(t.ParseGlob("templates/*.tmpl"))
err := t.Execute(os.Stdout, nil)
if err != nil {
log.Fatalf("template execution: %s", err)
}
} -
main.tmpl
{{template "header"}}
主要内容
{{template "footer"}}
-
header.html
{{define "header"}}
<!doctype html>
文档
{{end}} -
footor.html
{{define "footer"}}
{{end}}
还有其他有用的链接吗?
英文:
Tired this https://github.com/golang-samples/template/blob/master/parseglob/main.go but server is shutting down forcefully.
1)main.go
package main
import (
"log"
"os"
"text/template"
)
func main() {
t := template.New("main.tmpl")
t = template.Must(t.ParseGlob("templates/*.tmpl"))
err := t.Execute(os.Stdout, nil)
if err != nil {
log.Fatalf("template execution: %s", err)
}
}
-
main.tmpl
{{template "header"}}
<p>main content</p>
{{template "footer"}} -
header.html
{{define "header"}}
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{{end}} -
footor.html
{{define "footer"}}
</body>
</html>
{{end}}
Any other useful link ?
答案1
得分: 0
根据我的下面的代码,
我在这里创建了一个教程:
http://javatogo.blogspot.com/2013/06/howto-render-multiple-templates-with-go.html
这是一个渲染多个模板的示例:
const rootPageTemplateHtml = `
<!DOCTYPE html>
<html>
<head>
<title>{{.PageTitle}}</title>
</head>
<body>
{{template "pageMenu" .}}
{{template "pageContent" .}}
{{template "pageFooter" .}}
</body>
</html>
`
const pageMenuTemplateHtml = `
<div>
菜单:{{.PageName}}
</div>
`
type PageContent struct {
PageName string
PageContent interface{}
PageTitle string
}
func initTemplate(tmpl *template.Template) {
*tmpl = *template.Must(template.New("rootPage").Parse(rootPageTemplateHtml))
tmpl.New("pageHeader").Parse(``)
tmpl.New("pageMenu").Parse(pageMenuTemplateHtml)
tmpl.New("pageFooter").Parse(``)
}
func execTemplate(tmpl *template.Template, w *http.ResponseWriter, pc *PageContent) {
if err := tmpl.Execute(*w, *pc); err != nil {
http.Error(*w, err.Error(), http.StatusInternalServerError)
}
}
现在让我们添加第一个页面:
const welcomeTemplateHTML = `
<div>欢迎页面</div>
`
var welcomePage *template.Template
func initWelcomePageTemplate() {
if nil == welcomePage {
welcomePage = new(template.Template)
initTemplate(welcomePage)
welcomePage.New("pageContent").Parse(welcomeTemplateHTML)
}
}
func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
initWelcomePageTemplate()
execTemplate(welcomePage, w, pc)
}
和第二个页面:
const linksTemplateHTML = `
<div>第二个页面</div>
`
var secondPage *template.Template
func initSecondPageTemplate() {
if nil == secondPage {
secondPage = new(template.Template)
initTemplate(secondPage)
secondPage.New("pageContent").Parse(secondTemplateHTML)
}
}
func renderSecondPage(w *http.ResponseWriter, pc *PageContent) {
initSecondPageTemplate()
execTemplate(secondPage, w, pc)
}
要执行这些不同的模板,只需在处理程序中添加:
func init() {
http.HandleFunc("/", welcome)
http.HandleFunc("/second", second)
}
func welcome(w http.ResponseWriter, r *http.Request) {
pc := PageContent{"/", nil, "欢迎页面标题"}
renderWelcomePage(&w, &pc)
}
func second(w http.ResponseWriter, r *http.Request) {
pc := PageContent{"/second", nil, "第二个页面标题"}
renderSecondPage(&w, &pc)
}
现在您可以添加任意数量的文件。
英文:
based on my below code,
i created a howto here:
http://javatogo.blogspot.com/2013/06/howto-render-multiple-templates-with-go.html
here is an example how to render multiple templates:
const rootPageTemplateHtml = `
<!DOCTYPE html>
<html>
<head>
<title>{{.PageTitle}}</title>
</head>
<body>
{{template "pageMenu" .}}
{{template "pageContent" .}}
{{template "pageFooter" .}}
</body>
</html>
`
const pageMenuTemplateHtml = `
<div>
menu: {{.PageName}}
</div>
`
type PageContent struct {
PageName string
PageContent interface{}
PageTitle string
}
func initTemplate(tmpl *template.Template) {
*tmpl = *template.Must(template.New("rootPage").Parse(rootPageTemplateHtml))
tmpl.New("pageHeader").Parse(``)
tmpl.New("pageMenu").Parse(pageMenuTemplateHtml)
tmpl.New("pageFooter").Parse(``)
}
func execTemplate(tmpl *template.Template, w *http.ResponseWriter, pc *PageContent) {
if err := tmpl.Execute(*w, *pc); err != nil {
http.Error(*w, err.Error(), http.StatusInternalServerError)
}
}
now lets add the first page:
const welcomeTemplateHTML = `
<div>welcome page</div>
`
var welcomePage *template.Template
func initWelcomePageTemplate() {
if nil == welcomePage {
welcomePage = new(template.Template)
initTemplate(welcomePage)
welcomePage.New("pageContent").Parse(welcomeTemplateHTML)
}
}
func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
initWelcomePageTemplate()
execTemplate(welcomePage, w, pc)
}
and a second page:
const linksTemplateHTML = `
<div>second page</div>
`
var secondPage *template.Template
func initSecondPageTemplate() {
if nil == secondPage {
secondPage = new(template.Template)
initTemplate(secondPage)
secondPage.New("pageContent").Parse(secondTemplateHTML)
}
}
func renderSecondPage(w *http.ResponseWriter, pc *PageContent) {
initSecondPageTemplate()
execTemplate(secondPage, w, pc)
}
to execute this different templates just add to your handler:
func init() {
http.HandleFunc("/", welcome)
http.HandleFunc("/second", second)
}
func welcome(w http.ResponseWriter, r *http.Request) {
pc := PageContent{"/", nil, "Welcome Page Title"}
renderWelcomePage(&w, &pc)
}
func second(w http.ResponseWriter, r *http.Request) {
pc := PageContent{"/second", nil, "Second Page Title"}
renderSecondPage(&w, &pc)
}
now you can add as many files as you want
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论