I18N Support

The template engine supports i18n features, allowing you to inject specific i18n languages into the context to render different requests/pages in different i18n languages. For example:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/i18n/gi18n"
  7. )
  8. func main() {
  9. var (
  10. ctxCN = gi18n.WithLanguage(context.TODO(), "zh-CN")
  11. ctxJa = gi18n.WithLanguage(context.TODO(), "ja")
  12. content = `{{.name}} says "{#hello}{#world}!"`
  13. )
  14. result1, _ := g.View().ParseContent(ctxCN, content, g.Map{
  15. "name": "john",
  16. })
  17. fmt.Println(result1)
  18. result2, _ := g.View().ParseContent(ctxJa, content, g.Map{
  19. "name": "john",
  20. })
  21. fmt.Println(result2)
  22. }

After executing, the output result is: (ensure the current running directory contains i18n translation configuration files)

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

HTTP Object View

The WebServer return object in the goframe framework provides basic template parsing methods as follows:

  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

Among them, WriteTpl is used to output template files, and WriteTplContent is used to directly parse and output template content.

Example of use:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/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. }

After executing, the output result is:

  1. Cookie:default, Session:john

Controller View Management

The goframe framework provides good template engine support for the routing controller registration method, managed by the gmvc.View view object, providing good data isolation. The controller view is designed for concurrency safety, allowing asynchronous operations in multi-threading.

Template Engine - Others - 图1warning

The controller registration method is similar to the PHP execution process and relatively inefficient, so it is not recommended for future use.

  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{}))

Usage example 1:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. "github.com/gogf/gf/v2/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. }

The template content of index.tpl is as follows:

  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>

After executing, visiting http://127.0.0.1:8199/template/info shows the parsed template on the page. If the page reports an error that it cannot find the template file, don’t worry, because no template directory setting is made, the default is the current executable directory (/tmp for Linux&Mac, C:\Documents and Settings\Username\Local Settings\Temp for Windows).

The given template file parameter file requires a full filename suffix, such as index.tpl, index.html, etc. The template engine does not mandate the file extension, allowing full customization by the user. Additionally, the template file parameter also supports the absolute path of the file (complete file path).

Of course, we can also directly parse the template content, for example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. "github.com/gogf/gf/v2/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. }

After executing, visiting http://127.0.0.1:8199/template/info shows the parsed content as follows:

  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>