使用ParseGlob在golang中如何渲染超过两个模板?

huangapple go评论151阅读模式
英文:

With ParseGlob how to render more than two templates in golang?

问题

尝试了这个链接 https://github.com/golang-samples/template/blob/master/parseglob/main.go 但是服务器强制关闭了。

  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)
    }
    }

  2. main.tmpl

    {{template "header"}}

    主要内容

    {{template "footer"}}

  3. header.html

    {{define "header"}}
    <!doctype html>




    文档


    {{end}}

  4. 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

  1. import (
  2. &quot;log&quot;
  3. &quot;os&quot;
  4. &quot;text/template&quot;
  5. )
  6. func main() {
  7. t := template.New(&quot;main.tmpl&quot;)
  8. t = template.Must(t.ParseGlob(&quot;templates/*.tmpl&quot;))
  9. err := t.Execute(os.Stdout, nil)
  10. if err != nil {
  11. log.Fatalf(&quot;template execution: %s&quot;, err)
  12. }
  13. }
  1. main.tmpl

    {{template "header"}}
    <p>main content</p>
    {{template "footer"}}

  2. header.html

    {{define "header"}}
    <!doctype html>
    <html lang="ja">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    {{end}}

  3. 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

这是一个渲染多个模板的示例:

  1. const rootPageTemplateHtml = `
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;title&gt;{{.PageTitle}}&lt;/title&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. {{template &quot;pageMenu&quot; .}}
  9. {{template &quot;pageContent&quot; .}}
  10. {{template &quot;pageFooter&quot; .}}
  11. &lt;/body&gt;
  12. &lt;/html&gt;
  13. `
  14. const pageMenuTemplateHtml = `
  15. &lt;div&gt;
  16. 菜单:{{.PageName}}
  17. &lt;/div&gt;
  18. `
  19. type PageContent struct {
  20. PageName string
  21. PageContent interface{}
  22. PageTitle string
  23. }
  24. func initTemplate(tmpl *template.Template) {
  25. *tmpl = *template.Must(template.New(&quot;rootPage&quot;).Parse(rootPageTemplateHtml))
  26. tmpl.New(&quot;pageHeader&quot;).Parse(``)
  27. tmpl.New(&quot;pageMenu&quot;).Parse(pageMenuTemplateHtml)
  28. tmpl.New(&quot;pageFooter&quot;).Parse(``)
  29. }
  30. func execTemplate(tmpl *template.Template, w *http.ResponseWriter, pc *PageContent) {
  31. if err := tmpl.Execute(*w, *pc); err != nil {
  32. http.Error(*w, err.Error(), http.StatusInternalServerError)
  33. }
  34. }

现在让我们添加第一个页面:

  1. const welcomeTemplateHTML = `
  2. &lt;div&gt;欢迎页面&lt;/div&gt;
  3. `
  4. var welcomePage *template.Template
  5. func initWelcomePageTemplate() {
  6. if nil == welcomePage {
  7. welcomePage = new(template.Template)
  8. initTemplate(welcomePage)
  9. welcomePage.New(&quot;pageContent&quot;).Parse(welcomeTemplateHTML)
  10. }
  11. }
  12. func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
  13. initWelcomePageTemplate()
  14. execTemplate(welcomePage, w, pc)
  15. }

和第二个页面:

  1. const linksTemplateHTML = `
  2. &lt;div&gt;第二个页面&lt;/div&gt;
  3. `
  4. var secondPage *template.Template
  5. func initSecondPageTemplate() {
  6. if nil == secondPage {
  7. secondPage = new(template.Template)
  8. initTemplate(secondPage)
  9. secondPage.New(&quot;pageContent&quot;).Parse(secondTemplateHTML)
  10. }
  11. }
  12. func renderSecondPage(w *http.ResponseWriter, pc *PageContent) {
  13. initSecondPageTemplate()
  14. execTemplate(secondPage, w, pc)
  15. }

要执行这些不同的模板,只需在处理程序中添加:

  1. func init() {
  2. http.HandleFunc(&quot;/&quot;, welcome)
  3. http.HandleFunc(&quot;/second&quot;, second)
  4. }
  5. func welcome(w http.ResponseWriter, r *http.Request) {
  6. pc := PageContent{&quot;/&quot;, nil, &quot;欢迎页面标题&quot;}
  7. renderWelcomePage(&amp;w, &amp;pc)
  8. }
  9. func second(w http.ResponseWriter, r *http.Request) {
  10. pc := PageContent{&quot;/second&quot;, nil, &quot;第二个页面标题&quot;}
  11. renderSecondPage(&amp;w, &amp;pc)
  12. }

现在您可以添加任意数量的文件。

英文:

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:

  1. const rootPageTemplateHtml = `
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;title&gt;{{.PageTitle}}&lt;/title&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. {{template &quot;pageMenu&quot; .}}
  9. {{template &quot;pageContent&quot; .}}
  10. {{template &quot;pageFooter&quot; .}}
  11. &lt;/body&gt;
  12. &lt;/html&gt;
  13. `
  14. const pageMenuTemplateHtml = `
  15. &lt;div&gt;
  16. menu: {{.PageName}}
  17. &lt;/div&gt;
  18. `
  19. type PageContent struct {
  20. PageName string
  21. PageContent interface{}
  22. PageTitle string
  23. }
  24. func initTemplate(tmpl *template.Template) {
  25. *tmpl = *template.Must(template.New(&quot;rootPage&quot;).Parse(rootPageTemplateHtml))
  26. tmpl.New(&quot;pageHeader&quot;).Parse(``)
  27. tmpl.New(&quot;pageMenu&quot;).Parse(pageMenuTemplateHtml)
  28. tmpl.New(&quot;pageFooter&quot;).Parse(``)
  29. }
  30. func execTemplate(tmpl *template.Template, w *http.ResponseWriter, pc *PageContent) {
  31. if err := tmpl.Execute(*w, *pc); err != nil {
  32. http.Error(*w, err.Error(), http.StatusInternalServerError)
  33. }
  34. }

now lets add the first page:

  1. const welcomeTemplateHTML = `
  2. &lt;div&gt;welcome page&lt;/div&gt;
  3. `
  4. var welcomePage *template.Template
  5. func initWelcomePageTemplate() {
  6. if nil == welcomePage {
  7. welcomePage = new(template.Template)
  8. initTemplate(welcomePage)
  9. welcomePage.New(&quot;pageContent&quot;).Parse(welcomeTemplateHTML)
  10. }
  11. }
  12. func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
  13. initWelcomePageTemplate()
  14. execTemplate(welcomePage, w, pc)
  15. }

and a second page:

  1. const linksTemplateHTML = `
  2. &lt;div&gt;second page&lt;/div&gt;
  3. `
  4. var secondPage *template.Template
  5. func initSecondPageTemplate() {
  6. if nil == secondPage {
  7. secondPage = new(template.Template)
  8. initTemplate(secondPage)
  9. secondPage.New(&quot;pageContent&quot;).Parse(secondTemplateHTML)
  10. }
  11. }
  12. func renderSecondPage(w *http.ResponseWriter, pc *PageContent) {
  13. initSecondPageTemplate()
  14. execTemplate(secondPage, w, pc)
  15. }

to execute this different templates just add to your handler:

  1. func init() {
  2. http.HandleFunc(&quot;/&quot;, welcome)
  3. http.HandleFunc(&quot;/second&quot;, second)
  4. }
  5. func welcome(w http.ResponseWriter, r *http.Request) {
  6. pc := PageContent{&quot;/&quot;, nil, &quot;Welcome Page Title&quot;}
  7. renderWelcomePage(&amp;w, &amp;pc)
  8. }
  9. func second(w http.ResponseWriter, r *http.Request) {
  10. pc := PageContent{&quot;/second&quot;, nil, &quot;Second Page Title&quot;}
  11. renderSecondPage(&amp;w, &amp;pc)
  12. }

now you can add as many files as you want

huangapple
  • 本文由 发表于 2013年6月20日 18:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/17211027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定