Google app engine 使用ParseGlob,如何在golang中呈现两个以上的模板?
对此已厌倦,但服务器正在强制关闭。 1) 梅因,加油 包干管Google app engine 使用ParseGlob,如何在golang中呈现两个以上的模板?,google-app-engine,parsing,go,go-templates,Google App Engine,Parsing,Go,Go Templates,对此已厌倦,但服务器正在强制关闭。 1) 梅因,加油 包干管 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 {
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)
}
}
2) main.tmpl
{{template "header"}}
<p>main content</p>
{{template "footer"}}
{{template“header”}
主要内容
{{模板“页脚”}
3) header.html
{{define "header"}}
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{{end}}
{{define“header”}
文件
{{end}
4) footor.html
{{define "footer"}}
</body>
</html>
{{end}}
{{定义“页脚”}
{{end}
任何其他有用的链接?基于我下面的代码 我在这里创建了一个howto: 以下是如何呈现多个模板的示例:
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)
}
}
现在您可以添加任意数量的文件了@fmt.Println.MKO:是的,我以前也试过这个例子。。。但如果我呈现以下内容,那么它就是用html标记呈现的。这是我不想要的。例如:{{define“body”}hi{{end}}你是说用html呈现它是什么意思?你的输入是什么,输出是什么,你希望得到什么?/想要得到什么?包主导入(“html/template”“net/http”)函数处理程序(w http.ResponseWriter,r*http.Request){s1,:=template.ParseFiles(“templates/t1.html”,“templates/t2.html”,“templates/t3.html”)s1.ExecuteTemplate(w,“t_cd”,nil)s1.ExecuteTemplate(w,“body”,nil)}func init(){http.HandleFunc(“/”,HandleFunc)}输出:cdhi“输出中的html标记即将出现,我不想显示”.啊,那么你的3个模板是一个主模板、一个页脚模板和一个正文模板?你想让身体变成主体吗?在这种情况下,您需要这样做:
tmpl=template.Must(template.New(“main”).ParseFiles(“main.html”))tmpl.New(“header.html”)tmpl.New(“body”).ParseFiles(“body.html”)
其中main.html在您想要的头和体的点处有:{template“header”。}{{template“body”}}
header.html和body.html,不需要模板或define语句,只需要简单的html,或者一些应该在html中呈现的动态内容。如果你的应用程序是appengine应用程序,你不能使用模板文件,只需将html放在代码中的字符串中,并使用Parse而不是ParseFiles方法
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)
}
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)
}
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)
}