I18N变量

在模板引擎中内置了对i18n特性的支持,增加了I18nLanguage模板变量,当开发者为模板变量赋值该变量时,将会按照该变量指定的语言进行转译。该特性使得使用同一个模板引擎对象,针对于同一个页面,可以按照变量配置输出不同的国际化转译内容。例如:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/frame/g"
  5. )
  6. func main() {
  7. content := `{{.name}} says "a{#hello}{#world}!"`
  8. result1, _ := g.View().ParseContent(content, g.Map{
  9. "name": "john",
  10. "I18nLanguage": "zh-CN",
  11. })
  12. fmt.Println(result1)
  13. result2, _ := g.View().ParseContent(content, g.Map{
  14. "name": "john",
  15. "I18nLanguage": "ja",
  16. })
  17. fmt.Println(result2)
  18. }

执行后,输出结果为:(保证当前运行目录带有i18n转译配置文件)

  1. john says "你好世界!"
  2. john says "こんにちは世界!"

HTTP对象视图

gf框架的WebServer的返回对象中提供了基础的模板解析方法,如下:

  1. func (r *Response) WriteTpl(tpl string, params map[string]interface{}, funcMap ...map[string]interface{}) error
  2. func (r *Response) WriteTplContent(content string, params map[string]interface{}, funcMap ...map[string]interface{}) error

其中WriteTpl用于输出模板文件,WriteTplContent用于直接解析输出模板内容。

使用示例:

  1. package main
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. "github.com/gogf/gf/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Cookie.Set("theme", "default")
  10. r.Session.Set("name", "john")
  11. r.Response.WriteTplContent(`Cookie:{{.Cookie.theme}}, Session:{{.Session.name}}`, nil)
  12. })
  13. s.SetPort(8199)
  14. s.Run()
  15. }

执行后,输出结果为:

  1. Cookie:default, Session:john

控制器视图管理

gf为路由控制器注册方式提供了良好的模板引擎支持,由gmvc.View视图对象进行管理,提供了良好的数据隔离性。控制器视图是并发安全设计的,允许在多线程中异步操作。

控制器注册方式类似于PHP执行流程,相对来说性能比较低效,因此未来不再推荐使用控制器注册方式。

  1. func (view *View) Assign(key string, value interface{})
  2. func (view *View) Assigns(data gview.Params)
  3. func (view *View) Parse(file string) ([]byte, error)
  4. func (view *View) ParseContent(content string) ([]byte, error)
  5. func (view *View) Display(files ...string) error
  6. func (view *View) DisplayContent(content string) error
  7. func (view *View) LockFunc(f func(vars map[string]interface{}))
  8. func (view *View) RLockFunc(f func(vars map[string]interface{}))

使用示例1:

  1. package main
  2. import (
  3. "github.com/gogf/gf/net/ghttp"
  4. "github.com/gogf/gf/frame/gmvc"
  5. )
  6. type ControllerTemplate struct {
  7. gmvc.Controller
  8. }
  9. func (c *ControllerTemplate) Info() {
  10. c.View.Assign("name", "john")
  11. c.View.Assigns(map[string]interface{}{
  12. "age" : 18,
  13. "score" : 100,
  14. })
  15. c.View.Display("index.tpl")
  16. }
  17. func main() {
  18. s := ghttp.GetServer()
  19. s.BindController("/template", new(ControllerTemplate))
  20. s.SetPort(8199)
  21. s.Run()
  22. }

其中index.tpl的模板内容如下:

  1. <html>
  2. <head>
  3. <title>gf template engine</title>
  4. </head>
  5. <body>
  6. <p>Name: {{.name}}</p>
  7. <p>Age: {{.age}}</p>
  8. <p>Score:{{.score}}</p>
  9. </body>
  10. </html>

执行后,访问 http://127.0.0.1:8199/template/info 可以看到模板被解析并展示到页面上。如果页面报错找不到模板文件,没有关系,因为这里并没有对模板目录做设置,默认是当前可行文件的执行目录(Linux&Mac下是/tmp目录,Windows下是C:\Documents and Settings\用户名\Local Settings\Temp)。

其中,给定的模板文件file参数是需要带完整的文件名后缀,例如:index.tplindex.html等等,模板引擎对模板文件后缀名没有要求,用户可完全自定义。此外,模板文件参数也支持文件的绝对路径(完整的文件路径)。

当然,我们也可以直接解析模板内容,例如:

  1. package main
  2. import (
  3. "github.com/gogf/gf/net/ghttp"
  4. "github.com/gogf/gf/frame/gmvc"
  5. )
  6. type ControllerTemplate struct {
  7. gmvc.Controller
  8. }
  9. func (c *ControllerTemplate) Info() {
  10. c.View.Assign("name", "john")
  11. c.View.Assigns(map[string]interface{}{
  12. "age" : 18,
  13. "score" : 100,
  14. })
  15. c.View.DisplayContent(`
  16. <html>
  17. <head>
  18. <title>gf template engine</title>
  19. </head>
  20. <body>
  21. <p>Name: {{.name}}</p>
  22. <p>Age: {{.age}}</p>
  23. <p>Score:{{.score}}</p>
  24. </body>
  25. </html>
  26. `)
  27. }
  28. func main() {
  29. s := ghttp.GetServer()
  30. s.BindController("/template", new(ControllerTemplate{}))
  31. s.SetPort(8199)
  32. s.Run()
  33. }

执行后,访问 http://127.0.0.1:8199/template/info 可以看到解析后的内容如下:

  1. <html>
  2. <head>
  3. <title>gf template engine</title>
  4. </head>
  5. <body>
  6. <p>Name: john</p>
  7. <p>Age: 18</p>
  8. <p>Score:100</p>
  9. </body>
  10. </html>