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:
package main
import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/i18n/gi18n"
)
func main() {
var (
ctxCN = gi18n.WithLanguage(context.TODO(), "zh-CN")
ctxJa = gi18n.WithLanguage(context.TODO(), "ja")
content = `{{.name}} says "{#hello}{#world}!"`
)
result1, _ := g.View().ParseContent(ctxCN, content, g.Map{
"name": "john",
})
fmt.Println(result1)
result2, _ := g.View().ParseContent(ctxJa, content, g.Map{
"name": "john",
})
fmt.Println(result2)
}
After executing, the output result is: (ensure the current running directory contains i18n
translation configuration files)
john says "你好世界!"
john says "こんにちは世界!"
HTTP Object View
The WebServer
return object in the goframe
framework provides basic template parsing methods as follows:
func (r *Response) WriteTpl(tpl string, params map[string]interface{}, funcMap ...map[string]interface{}) error
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:
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Cookie.Set("theme", "default")
r.Session.Set("name", "john")
r.Response.WriteTplContent(`Cookie:{{.Cookie.theme}}, Session:{{.Session.name}}`, nil)
})
s.SetPort(8199)
s.Run()
}
After executing, the output result is:
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.
warning
The controller registration method is similar to the PHP execution process and relatively inefficient, so it is not recommended for future use.
func (view *View) Assign(key string, value interface{})
func (view *View) Assigns(data gview.Params)
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{}))
Usage example 1:
package main
import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/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", new(ControllerTemplate))
s.SetPort(8199)
s.Run()
}
The template content of index.tpl
is as follows:
<html>
<head>
<title>gf template engine</title>
</head>
<body>
<p>Name: {{.name}}</p>
<p>Age: {{.age}}</p>
<p>Score:{{.score}}</p>
</body>
</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:
package main
import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/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", new(ControllerTemplate{}))
s.SetPort(8199)
s.Run()
}
After executing, visiting http://127.0.0.1:8199/template/info shows the parsed content as follows:
<html>
<head>
<title>gf template engine</title>
</head>
<body>
<p>Name: john</p>
<p>Age: 18</p>
<p>Score:100</p>
</body>
</html>