gf框架之模板引擎 - 基本用法
文章来源:http://gf.johng.cn/591642 控制器视图gf为控制器提供了良好的模板引擎支持,由 func (view *View) Assign(key string,value interface{}) func (view *View) Assigns(data map[string]interface{}) func (view *View) Parse(file string) ([]byte,error) func (view *View) ParseContent(content string) ([]byte,error) func (view *View) Display(files ...string) error func (view *View) DisplayContent(content string) error func (view *View) LockFunc(f func(vars map[string]interface{})) func (view *View) RLockFunc(f func(vars map[string]interface{})) 使用示例1: package main import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gmvc" ) type ControllerTemplate struct { gmvc.Controller } func (c *ControllerTemplate) Info() { c.View.Assign("name","john") c.View.Assigns(map[string]interface{}{ "age" : 18,"score" : 100,}) c.View.Display("index.tpl") } func main() { s := ghttp.GetServer() s.BindController("/template",&ControllerTemplate{}) s.SetPort(8199) s.Run() } 其中 <html> <head> <title>gf template engine</title> </head> <body> <p>Name: {{.name}}</p> <p>Age: {{.age}}</p> <p>Score:{{.score}}</p> </body> </html> 执行后,访问 其中,给定的模板文件file参数是需要带完整的文件名后缀,例如: 当然,我们也可以直接解析模板内容,请看示例2: package main import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gmvc" ) type ControllerTemplate struct { gmvc.Controller } func (c *ControllerTemplate) Info() { c.View.Assign("name",}) c.View.DisplayContent(` <html> <head> <title>gf template engine</title> </head> <body> <p>Name: {{.name}}</p> <p>Age: {{.age}}</p> <p>Score:{{.score}}</p> </body> </html> `) } func main() { s := ghttp.GetServer() s.BindController("/template",&ControllerTemplate{}) s.SetPort(8199) s.Run() } 执行后,访问 <html> <head> <title>gf template engine</title> </head> <body> <p>Name: john</p> <p>Age: 18</p> <p>Score:100</p> </body> </html> 非控制器视图非控制器中使用模板引擎没有控制器视图的支持,可以使用底层的gview包来实现,可以通过单例管理器来获取默认的单例gview对象。 gview包方法列表: func Get(path string) *View func New(path string) *View func (view *View) BindFunc(name string,function interface{}) func (view *View) Parse(file string,params map[string]interface{}) ([]byte,error) func (view *View) ParseContent(content string,error) func (view *View) GetPath() string func (view *View) SetPath(path string) 使用示例1: package main import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gins" ) func main() { s := ghttp.GetServer() s.BindHandler("/template2",func(r *ghttp.Request){ content,_ := gins.View().Parse("index.tpl",map[string]interface{}{ "id" : 123,"name" : "john",}) r.Response.Write(content) }) s.SetPort(8199) s.Run() } 在这个示例中我们使用单例管理器获取一个默认的视图对象,随后通过该视图渲染对应模板目录下的 我们也可以通过 当然,也可以直接解析模板内容,使用示例2: package main import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gins" ) func main() { s := ghttp.GetServer() s.BindHandler("/template2",func(r *ghttp.Request){ tplcontent := `id:{{.id}},name:{{.name}}` content,_ := gins.View().ParseContent(tplcontent,}) r.Response.Write(content) }) s.SetPort(8199) s.Run() } 执行后,访问 此外,需要注意的是,虽然以上示例都是在Web Server中进行展示的,但是模板引擎是对模板文件/内容的智能解析,可以用在任何的场景中。 修改模板目录模板引擎作为gf框架的核心组件,可以通过以下方式修改模板引擎的默认模板文件查找目录:
例如,我们的执行程序文件为main,那么可以通过以下方式修改模板引擎的模板目录(Linux下):
自动检测更新模板引擎使用了缓存机制,当模板文件第一次被读取后会被缓存到内存,下一次读取时将会直接从缓存中获取,以提高执行效率。并且,模板引擎提供了对模板文件的自动检测更新机制,当模板文件在外部被修改后,模板引擎能够即时地监控到并刷新模板文件的缓存内容。 模板引擎的自动检测更新机制也是gf框架的一大特色。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |