gview增加模板变量分隔符设置方法SetDelimiters; bug fix: ILUUA

This commit is contained in:
john
2018-08-06 12:39:55 +08:00
parent c2ba52f584
commit f2db13df9c
11 changed files with 59 additions and 21 deletions

View File

@ -103,7 +103,11 @@ func IsDir(path string) bool {
// 判断所给路径是否为文件
func IsFile(path string) bool {
return !IsDir(path)
s, err := os.Stat(path)
if err != nil {
return false
}
return !s.IsDir()
}
// 获取文件或目录信息

View File

@ -4,7 +4,7 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 视图管理
// 视图管理.
package gview
import (
@ -22,10 +22,11 @@ import (
// 视图对象
type View struct {
mu sync.RWMutex
paths *gspath.SPath // 模板查找目录(绝对路径)
funcmap map[string]interface{} // FuncMap
contents *gmap.StringStringMap // 已解析的模板文件内容
mu sync.RWMutex
paths *gspath.SPath // 模板查找目录(绝对路径)
funcmap map[string]interface{} // FuncMap
contents *gmap.StringStringMap // 已解析的模板文件内容
delimiters []string // 模板变量分隔符号
}
// 视图表
@ -59,10 +60,12 @@ func New(path string) *View {
s := gspath.New()
s.Set(path)
view := &View {
paths : s,
funcmap : make(map[string]interface{}),
contents : gmap.NewStringStringMap(),
paths : s,
funcmap : make(map[string]interface{}),
contents : gmap.NewStringStringMap(),
delimiters : make([]string, 2),
}
view.SetDelimiters("{{", "}}")
view.BindFunc("include", view.funcInclude)
return view
}
@ -95,7 +98,7 @@ func (view *View) Parse(file string, params map[string]interface{}) ([]byte, err
view.mu.RLock()
defer view.mu.RUnlock()
buffer := bytes.NewBuffer(nil)
if tpl, err := template.New(path).Funcs(view.funcmap).Parse(content); err != nil {
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 {
@ -111,7 +114,7 @@ func (view *View) ParseContent(content string, params map[string]interface{}) ([
defer view.mu.RUnlock()
name := gconv.String(ghash.BKDRHash64([]byte(content)))
buffer := bytes.NewBuffer(nil)
if tpl, err := template.New(name).Funcs(view.funcmap).Parse(content); err != nil {
if tpl, err := template.New(name).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 {
@ -121,6 +124,12 @@ func (view *View) ParseContent(content string, params map[string]interface{}) ([
return buffer.Bytes(), nil
}
// 设置模板变量解析分隔符号
func (view *View) SetDelimiters(left, right string) {
view.delimiters[0] = left
view.delimiters[1] = right
}
// 绑定自定义函数,该函数是全局有效,即调用之后每个线程都会生效,因此有并发安全控制
func (view *View) BindFunc(name string, function interface{}) {
view.mu.Lock()

View File

@ -17,7 +17,7 @@ type Controller struct {
// 测试模板热更新机制
func (c *Controller) Test() {
b, _ := c.View.Parse("test.tpl")
b, _ := c.View.Parse("gview.tpl")
c.Response.Write(b)
}

View File

@ -8,7 +8,9 @@ import (
func main() {
v := g.View()
b, err := v.Parse("test.tpl", nil)
b, err := v.Parse("gview.tpl", map[string]interface{} {
"k" : "v",
})
fmt.Println(err)
fmt.Println(b)
fmt.Println(string(b))
}

1
geg/os/gview/gview.tpl Normal file
View File

@ -0,0 +1 @@
test.tpl content, vars: {{.}}

View File

@ -0,0 +1,17 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g"
)
func main() {
v := g.View()
v.SetDelimiters("${", "}")
b, err := v.Parse("gview_delimiters.tpl", map[string]interface{} {
"k" : "v",
})
fmt.Println(err)
fmt.Println(string(b))
}

View File

@ -0,0 +1 @@
test.tpl content, vars: ${.}

View File

@ -12,7 +12,7 @@ func main() {
v := g.View()
v.SetPath(`D:\Workspace\Go\GOPATH\src\gitee.com\johng\gf\geg\os\gview`)
gtime.SetInterval(time.Second, func() bool {
b, _ := v.Parse("test.tpl", nil)
b, _ := v.Parse("gview.tpl", nil)
fmt.Println(string(b))
return true
})

View File

@ -1 +0,0 @@
test.tpl content2

View File

@ -10,7 +10,7 @@ func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
g.View().SetPath(`D:\Workspace\Go\GOPATH\src\gitee.com\johng\gf\geg\os\gview`)
b, _ := g.View().Parse("test.tpl", nil)
b, _ := g.View().Parse("gview.tpl", nil)
r.Response.Write(b)
})
s.Run()

View File

@ -1,10 +1,15 @@
package main
import (
"fmt"
"math"
"gitee.com/johng/gf/g/os/gfile"
)
func main() {
fmt.Println(int(math.MaxInt64))
}
path := "/home/john/Documents/temp"
flags1 := gfile.IsFile(path)
if flags1 == true {
println("有")
} else {
println("无")
}
}