diff --git a/g/net/ghttp/ghttp_response.go b/g/net/ghttp/ghttp_response.go index cf096efb5..db5b3e41d 100644 --- a/g/net/ghttp/ghttp_response.go +++ b/g/net/ghttp/ghttp_response.go @@ -14,6 +14,7 @@ import ( "gitee.com/johng/gf/g/encoding/gparser" "strconv" "fmt" + "gitee.com/johng/gf/g/frame/gins" ) // 服务端请求返回对象 @@ -136,6 +137,15 @@ func (r *Response) WriteXml(content interface{}, rootTag...string) error { return nil } +func (r *Response) Template(tpl string, params...map[string]interface{}) error { + if b, err := gins.View().Parse(tpl, params...); err != nil { + return err + } else { + r.Write(b) + } + return nil +} + // 允许AJAX跨域访问 func (r *Response) SetAllowCrossDomainRequest(allowOrigin string, allowMethods string, maxAge...int) { age := 3628800 diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index b13b6398b..7b979dd4e 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -101,6 +101,12 @@ func IsDir(path string) bool { return s.IsDir() } +// 获取当前工作目录 +func Pwd() string { + pwd, _ := filepath.Abs(filepath.Dir(os.Args[0])) + return pwd +} + // 判断所给路径是否为文件 func IsFile(path string) bool { s, err := os.Stat(path) diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index 9352c4c48..d9718a605 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -34,6 +34,12 @@ func New () *SPath { // 设置搜索路径,只保留当前设置项,其他搜索路径被清空 func (sp *SPath) Set(path string) error { r := gfile.RealPath(path) + if r == "" { + r = sp.Search(path) + if r == "" { + r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) + } + } if r != "" && gfile.IsDir(r) { sp.mu.Lock() sp.paths = []string{strings.TrimRight(r, gfile.Separator)} @@ -47,6 +53,12 @@ func (sp *SPath) Set(path string) error { // 添加搜索路径 func (sp *SPath) Add(path string) error { r := gfile.RealPath(path) + if r == "" { + r = sp.Search(path) + if r == "" { + r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path) + } + } if r != "" && gfile.IsDir(r) { sp.mu.Lock() sp.paths = append(sp.paths, r) diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 6005660c0..f4ccdb84b 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -81,7 +81,7 @@ func (view *View) AddPath(path string) error { } // 解析模板,返回解析后的内容 -func (view *View) Parse(file string, params map[string]interface{}) ([]byte, error) { +func (view *View) Parse(file string, params...map[string]interface{}) ([]byte, error) { path := view.paths.Search(file) content := view.contents.Get(path) if content == "" { @@ -94,6 +94,11 @@ func (view *View) Parse(file string, params map[string]interface{}) ([]byte, err if content == "" { return nil, errors.New("tpl \"" + file + "\" not found") } + // 模板参数 + data := (map[string]interface{})(nil) + if len(params) > 0 { + data = params[0] + } // 执行模板解析,互斥锁主要是用于funcmap view.mu.RLock() defer view.mu.RUnlock() @@ -101,7 +106,7 @@ func (view *View) Parse(file string, params map[string]interface{}) ([]byte, err if tpl, err := template.New(path).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcmap).Parse(content); err != nil { return nil, err } else { - if err := tpl.Execute(buffer, params); err != nil { + if err := tpl.Execute(buffer, data); err != nil { return nil, err } } diff --git a/geg/other/test.go b/geg/other/test.go index b486b991c..51c69f579 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,16 +1,13 @@ package main import ( - "gitee.com/johng/gf/g/util/gregex" "fmt" - "gitee.com/johng/gf/g/util/gutil" + "path/filepath" + "os" ) func main() { - s := `username @ required|length:6,30 # 请输入用户名称|用户名称长度非法` - match, err := gregex.MatchString(`\s*((\w+)\s*@){0,1}\s*([^#]+)\s*(#\s*(.*)){0,1}\s*`, s) - fmt.Println(err) - gutil.Dump(match) + fmt.Println(filepath.Abs(filepath.Dir(os.Args[0]))) } \ No newline at end of file