diff --git a/.example/net/ghttp/server/controller/template/footer.html b/.example/net/ghttp/server/controller/template/footer.html new file mode 100644 index 000000000..83ae25b65 --- /dev/null +++ b/.example/net/ghttp/server/controller/template/footer.html @@ -0,0 +1 @@ +

FOOTER

\ No newline at end of file diff --git a/.example/net/ghttp/server/controller/template/header.html b/.example/net/ghttp/server/controller/template/header.html new file mode 100644 index 000000000..b9cb0a77c --- /dev/null +++ b/.example/net/ghttp/server/controller/template/header.html @@ -0,0 +1 @@ +

HEADER

\ No newline at end of file diff --git a/.example/net/ghttp/server/controller/template/layout.html b/.example/net/ghttp/server/controller/template/layout.html new file mode 100644 index 000000000..9f58c21a0 --- /dev/null +++ b/.example/net/ghttp/server/controller/template/layout.html @@ -0,0 +1,3 @@ +{{include "header.html" .}} +{{include .mainTpl .}} +{{include "footer.html" .}} \ No newline at end of file diff --git a/.example/net/ghttp/server/controller/template/main/main1.html b/.example/net/ghttp/server/controller/template/main/main1.html new file mode 100644 index 000000000..fdb0016f3 --- /dev/null +++ b/.example/net/ghttp/server/controller/template/main/main1.html @@ -0,0 +1 @@ +

MAIN1

\ No newline at end of file diff --git a/.example/net/ghttp/server/controller/template/main/main2.html b/.example/net/ghttp/server/controller/template/main/main2.html new file mode 100644 index 000000000..608512269 --- /dev/null +++ b/.example/net/ghttp/server/controller/template/main/main2.html @@ -0,0 +1 @@ +

MAIN2

\ No newline at end of file diff --git a/.example/net/ghttp/server/controller/view.go b/.example/net/ghttp/server/controller/view.go new file mode 100644 index 000000000..19e722ce9 --- /dev/null +++ b/.example/net/ghttp/server/controller/view.go @@ -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() +} diff --git a/i18n/gi18n/gi18n_manager.go b/i18n/gi18n/gi18n_manager.go index 426464843..e1738dca1 100644 --- a/i18n/gi18n/gi18n_manager.go +++ b/i18n/gi18n/gi18n_manager.go @@ -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, diff --git a/net/ghttp/ghttp_response_view.go b/net/ghttp/ghttp_response_view.go index 8878764f1..e2ae43659 100644 --- a/net/ghttp/ghttp_response_view.go +++ b/net/ghttp/ghttp_response_view.go @@ -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...)) } // 内置变量/对象 diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 294b1b8c6..946af2148 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -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", diff --git a/os/gfile/gfile.go b/os/gfile/gfile.go index 0f98622ca..c32e297bb 100644 --- a/os/gfile/gfile.go +++ b/os/gfile/gfile.go @@ -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 exist. func Exists(path string) bool { if _, err := os.Stat(path); !os.IsNotExist(err) {