I18N支持

模板引擎支持i18n特性,可以通过给上下文注入特定的i18n语言来实现不同的请求/页面使用不同的i18n语言渲染。例如:

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

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

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

HTTP对象视图

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

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

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

使用示例:

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

执行后,输出结果为:

Cookie:default, Session:john

控制器视图管理

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

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

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

使用示例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()
}

其中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.tplindex.html等等,模板引擎对模板文件后缀名没有要求,用户可完全自定义。此外,模板文件参数也支持文件的绝对路径(完整的文件路径)。

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

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

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

<html>
    <head>
        <title>gf template engine</title>
    </head>
    <body>
        <p>Name: john</p>
        <p>Age:  18</p>
        <p>Score:100</p>
    </body>
</html>







Content Menu

  • No labels