一、template嵌套

package main

import (
	"os"
	"text/template"
)

type title struct {
	Title string
}
func main(){
	header :=`{{ define "header"}}<head><meta charset="utf-8"/><title>{{ .Title }}</title></head>{{ end }}`
	page1 :=`{{ define "page1" }}<!DOCYPE html>
   <html>
        {{ template "header" . }}
        <body>
         this is page1
        </body>
   </html>
   {{ end }}
    `
	page2 :=`{{ define "page2"}}<!DOCYPE html>
   <html>
       {{ template "header" . }}
       <body>
        this is page2
       </body>
   </html>
   {{ end }}
   `
	var ti1 =title{"定义page1"}
	tp1,_ :=template.New("tp1").Parse(header)
	tp1,_ = tp1.Parse(page1)
	tp1,_ = tp1.Parse(page2)
	tp1.ExecuteTemplate(os.Stdout,"page1",ti1)

}

  

相关文章:

  • 2021-09-02
  • 2021-12-07
  • 2021-10-07
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
猜你喜欢
  • 2022-03-05
  • 2021-10-19
  • 2021-10-24
  • 2021-11-15
  • 2021-09-21
  • 2022-12-23
  • 2021-11-22
相关资源
相似解决方案