首页 > 其他 > 详细

template使用语法

时间:2021-06-23 09:30:36      阅读:22      评论:0      收藏:0      [点我收藏+]

一、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)

}

  

template使用语法

原文:https://www.cnblogs.com/wuchangblog/p/14920965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!