文章来源:http://gf.johng.cn/591642html
gf为控制器提供了良好的模板引擎支持,由gmvc.View
视图对象进行管理,提供了良好的数据隔离性。控制器视图是并发安全设计的,容许在多线程中异步操做。git
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:shell
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() }
其中index.tpl
的模板内容以下:缓存
<html> <head> <title>gf template engine</title> </head> <body> <p>Name: {{.name}}</p> <p>Age: {{.age}}</p> <p>Score:{{.score}}</p> </body> </html>
执行后,访问http://127.0.0.1:8199/template/info
能够看到模板被解析并展现到页面上。若是页面报错找不到模板文件,没有关系,由于这里并无对模板目录作设置,默认是当前可行文件的执行目录(Linux&Mac下是/tmp
目录,Windows下是C:\Documents and Settings\用户名\Local Settings\Temp
)。如何手动设置模板文件目录请查看后续章节,随后可回过头来手动修改目录后看到结果。安全
其中,给定的模板文件file参数是须要带完整的文件名后缀,例如:index.tpl
,index.html
等等,模板引擎对模板文件后缀名没有要求,用户可彻底自定义。此外,模板文件参数也支持文件的绝对路径(完整的文件路径)。多线程
固然,咱们也能够直接解析模板内容,请看示例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", "john") c.View.Assigns(map[string]interface{}{ "age" : 18, "score" : 100, }) 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() }
执行后,访问http://127.0.0.1:8199/template/info
能够看到解析后的内容以下:mvc
<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, params map[string]interface{}) ([]byte, 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() }
在这个示例中咱们使用单例管理器获取一个默认的视图对象,随后经过该视图渲染对应模板目录下的index.tpl
模板文件并给定模板变量参数。
咱们也能够经过SetPath
方法中心指定视图对象的模板目录,该方法是并发安全的,可是须要注意一旦改变了该视图对象的模板目录,将会在整个进程中生效。
固然,也能够直接解析模板内容,使用示例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, map[string]interface{}{ "id" : 123, "name" : "john", }) r.Response.Write(content) }) s.SetPort(8199) s.Run() }
执行后,访问http://127.0.0.1:8199/template2
能够看到解析后的内容为:id:123, name:john
此外,须要注意的是,虽然以上示例都是在Web Server中进行展现的,可是模板引擎是对模板文件/内容的智能解析,能够用在任何的场景中。
模板引擎做为gf框架的核心组件,能够经过如下方式修改模板引擎的默认模板文件查找目录:
SetPath
方法手动修改viewpath
gf.viewpath
例如,咱们的执行程序文件为main,那么能够经过如下方式修改模板引擎的模板目录(Linux下):
(推荐)经过单例模式
gins.View().SetPath("/opt/template")
经过命令行参数
./main --viewpath=/opt/template/
经过环境变量
启动时修改环境变量:
gf.viewpath=/opt/config/; ./main
使用genv包来修改环境变量:
genv.Set("gf.viewpath", "/opt/template")
模板引擎使用了缓存机制,当模板文件第一次被读取后会被缓存到内存,下一次读取时将会直接从缓存中获取,以提升执行效率。而且,模板引擎提供了对模板文件的自动检测更新机制,当模板文件在外部被修改后,模板引擎可以即时地监控到并刷新模板文件的缓存内容。
模板引擎的自动检测更新机制也是gf框架的一大特点。