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

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

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

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;text/template&quot;
)

func main() {
	t := template.New(&quot;main.tmpl&quot;)
	t = template.Must(t.ParseGlob(&quot;templates/*.tmpl&quot;))
	err := t.Execute(os.Stdout, nil)
	if err != nil {
		log.Fatalf(&quot;template execution: %s&quot;, err)
	}
}
  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

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

const rootPageTemplateHtml = `
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;{{.PageTitle}}&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    {{template &quot;pageMenu&quot; .}}
    {{template &quot;pageContent&quot; .}}
    {{template &quot;pageFooter&quot; .}}
  &lt;/body&gt;
&lt;/html&gt;
`
const pageMenuTemplateHtml = `
&lt;div&gt;
菜单:{{.PageName}}
&lt;/div&gt;
`

type PageContent struct {
  PageName    string
  PageContent interface{}
  PageTitle   string
}


func initTemplate(tmpl *template.Template) {
  *tmpl = *template.Must(template.New(&quot;rootPage&quot;).Parse(rootPageTemplateHtml))
  tmpl.New(&quot;pageHeader&quot;).Parse(``)
  tmpl.New(&quot;pageMenu&quot;).Parse(pageMenuTemplateHtml)
  tmpl.New(&quot;pageFooter&quot;).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 = `
&lt;div&gt;欢迎页面&lt;/div&gt;
`

var welcomePage *template.Template

func initWelcomePageTemplate() {
  if nil == welcomePage {
    welcomePage = new(template.Template)
    initTemplate(welcomePage)
    welcomePage.New(&quot;pageContent&quot;).Parse(welcomeTemplateHTML)
  }
}

func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
  initWelcomePageTemplate()
  execTemplate(welcomePage, w, pc)
}

和第二个页面:

const linksTemplateHTML = `
&lt;div&gt;第二个页面&lt;/div&gt;
`

var secondPage *template.Template

func initSecondPageTemplate() {
  if nil == secondPage {
    secondPage = new(template.Template)
    initTemplate(secondPage)
    secondPage.New(&quot;pageContent&quot;).Parse(secondTemplateHTML)
  }
}

func renderSecondPage(w *http.ResponseWriter, pc *PageContent) {
  initSecondPageTemplate()
  execTemplate(secondPage, w, pc)
}

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

func init() {
  http.HandleFunc(&quot;/&quot;, welcome)
  http.HandleFunc(&quot;/second&quot;, second)
}

func welcome(w http.ResponseWriter, r *http.Request) {
	pc := PageContent{&quot;/&quot;, nil, &quot;欢迎页面标题&quot;}
	renderWelcomePage(&amp;w, &amp;pc)
}

func second(w http.ResponseWriter, r *http.Request) {
	pc := PageContent{&quot;/second&quot;, nil, &quot;第二个页面标题&quot;}
	renderSecondPage(&amp;w, &amp;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 = `
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;{{.PageTitle}}&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    {{template &quot;pageMenu&quot; .}}
    {{template &quot;pageContent&quot; .}}
    {{template &quot;pageFooter&quot; .}}
  &lt;/body&gt;
&lt;/html&gt;
`
const pageMenuTemplateHtml = `
&lt;div&gt;
menu: {{.PageName}}
&lt;/div&gt;
`

type PageContent struct {
  PageName    string
  PageContent interface{}
  PageTitle   string
}


func initTemplate(tmpl *template.Template) {
  *tmpl = *template.Must(template.New(&quot;rootPage&quot;).Parse(rootPageTemplateHtml))
  tmpl.New(&quot;pageHeader&quot;).Parse(``)
  tmpl.New(&quot;pageMenu&quot;).Parse(pageMenuTemplateHtml)
  tmpl.New(&quot;pageFooter&quot;).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 = `
&lt;div&gt;welcome page&lt;/div&gt;
`

var welcomePage *template.Template

func initWelcomePageTemplate() {
  if nil == welcomePage {
    welcomePage = new(template.Template)
    initTemplate(welcomePage)
    welcomePage.New(&quot;pageContent&quot;).Parse(welcomeTemplateHTML)
  }
}

func renderWelcomePage(w *http.ResponseWriter, pc *PageContent) {
  initWelcomePageTemplate()
  execTemplate(welcomePage, w, pc)
}

and a second page:

const linksTemplateHTML = `
&lt;div&gt;second page&lt;/div&gt;
`

var secondPage *template.Template

func initSecondPageTemplate() {
  if nil == secondPage {
    secondPage = new(template.Template)
    initTemplate(secondPage)
    secondPage.New(&quot;pageContent&quot;).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(&quot;/&quot;, welcome)
  http.HandleFunc(&quot;/second&quot;, second)
}

func welcome(w http.ResponseWriter, r *http.Request) {
	pc := PageContent{&quot;/&quot;, nil, &quot;Welcome Page Title&quot;}
	renderWelcomePage(&amp;w, &amp;pc)
}

func second(w http.ResponseWriter, r *http.Request) {
	pc := PageContent{&quot;/second&quot;, nil, &quot;Second Page Title&quot;}
	renderSecondPage(&amp;w, &amp;pc)
}

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:

确定