improving gview with i18n

This commit is contained in:
John
2019-09-09 22:56:54 +08:00
parent 77a727e1d7
commit 7f943c6b5f
10 changed files with 52 additions and 5 deletions

View File

@ -0,0 +1 @@
<h1>FOOTER</h1>

View File

@ -0,0 +1 @@
<h1>HEADER</h1>

View File

@ -0,0 +1,3 @@
{{include "header.html" .}}
{{include .mainTpl .}}
{{include "footer.html" .}}

View File

@ -0,0 +1 @@
<h1>MAIN1</h1>

View File

@ -0,0 +1 @@
<h1>MAIN2</h1>

View File

@ -0,0 +1,26 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/frame/gmvc"
)
type Controller struct {
gmvc.Controller
}
func (c *Controller) Index() {
c.View.Display("layout.html")
}
// 不符合规范,不会被自动注册
func (c *Controller) Test(value interface{}) {
c.View.Display("layout.html")
}
func main() {
s := g.Server()
s.BindController("/view", new(Controller))
s.SetPort(8199)
s.Run()
}

View File

@ -44,6 +44,7 @@ var (
defaultDelimiters = []string{"{#", "}"}
)
// New creates and returns a new i18n manager.
func New(options ...Options) *Manager {
var opts Options
if len(options) > 0 {
@ -64,13 +65,16 @@ func New(options ...Options) *Manager {
}
}
// DefaultOptions returns the default options for i18n manager.
func DefaultOptions() Options {
path := "i18n"
realPath, _ := gfile.Search(path)
if realPath != "" {
path = realPath
} else {
path = "/" + path
// To avoid of the source of GF: github.com/gogf/i18n/gi18n
if gfile.Exists(path + gfile.Separator + "gi18n") {
path = ""
}
}
return Options{
Path: path,

View File

@ -36,12 +36,18 @@ func (r *Response) WriteTplContent(content string, params ...gview.Params) error
// 解析模板文件,并返回模板内容
func (r *Response) ParseTpl(tpl string, params ...gview.Params) (string, error) {
return r.Server.config.View.Parse(tpl, r.buildInVars(params...))
if r.Server.config.View != nil {
return r.Server.config.View.Parse(tpl, r.buildInVars(params...))
}
return gview.Instance().Parse(tpl, r.buildInVars(params...))
}
// 解析并返回模板内容
func (r *Response) ParseTplContent(content string, params ...gview.Params) (string, error) {
return r.Server.config.View.ParseContent(content, r.buildInVars(params...))
if r.Server.config.View != nil {
return r.Server.config.View.ParseContent(content, r.buildInVars(params...))
}
return gview.Instance().ParseContent(content, r.buildInVars(params...))
}
// 内置变量/对象

View File

@ -88,7 +88,6 @@ var defaultServerConfig = ServerConfig{
IdleTimeout: 60 * time.Second,
MaxHeaderBytes: 1024,
KeepAlive: true,
View: gview.Instance(),
IndexFiles: []string{"index.html", "index.htm"},
IndexFolder: false,
ServerAgent: "gf http server",

View File

@ -84,6 +84,11 @@ func OpenWithFlagPerm(path string, flag int, perm int) (*os.File, error) {
return f, nil
}
// Join joins string array paths with file separator of current system.
func Join(paths ...string) string {
return strings.Join(paths, Separator)
}
// Exists checks whether given <path> exist.
func Exists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {