文章來源: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框架的一大特點。