diff --git a/os/gview/gview_config.go b/os/gview/gview_config.go index 1ce7767be..2df6f5f22 100644 --- a/os/gview/gview_config.go +++ b/os/gview/gview_config.go @@ -44,6 +44,9 @@ func (view *View) SetConfig(config Config) error { if len(config.Delimiters) > 1 { view.SetDelimiters(config.Delimiters[0], config.Delimiters[1]) } + // Clear global template object cache. + // It's just cache, do not hesitate clearing it. + templates.Clear() return nil } @@ -199,6 +202,8 @@ func (view *View) SetDelimiters(left, right string) { // The is the function name which can be called in template content. func (view *View) BindFunc(name string, function interface{}) { view.funcMap[name] = function + // Clear global template object cache. + templates.Clear() } // BindFuncMap registers customized global template functions by map to current view object. @@ -208,6 +213,8 @@ func (view *View) BindFuncMap(funcMap FuncMap) { for k, v := range funcMap { view.funcMap[k] = v } + // Clear global template object cache. + templates.Clear() } // SetI18n binds i18n manager to current view engine. diff --git a/os/gview/gview_doparse.go b/os/gview/gview_doparse.go index 348283416..811d8d8b8 100644 --- a/os/gview/gview_doparse.go +++ b/os/gview/gview_doparse.go @@ -146,8 +146,9 @@ func (view *View) ParseDefault(params ...Params) (result string, err error) { // and returns the parsed content in []byte. func (view *View) ParseContent(content string, params ...Params) (string, error) { err := (error)(nil) - tpl := templates.GetOrSetFuncLock(gCONTENT_TEMPLATE_NAME, func() interface{} { - return template.New(gCONTENT_TEMPLATE_NAME).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap) + key := fmt.Sprintf("%s_%v", gCONTENT_TEMPLATE_NAME, view.delimiters) + tpl := templates.GetOrSetFuncLock(key, func() interface{} { + return template.New(key).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap) }).(*template.Template) // Using memory lock to ensure concurrent safety for content parsing. hash := strconv.FormatUint(ghash.DJBHash64([]byte(content)), 10) @@ -202,8 +203,9 @@ func (view *View) ParseContent(content string, params ...Params) (string, error) // with the same given . It will also automatically refresh the template cache // if the template files under changes (recursively). func (view *View) getTemplate(path string, pattern string) (tpl *template.Template, err error) { - r := templates.GetOrSetFuncLock(path, func() interface{} { - tpl = template.New(path).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap) + key := fmt.Sprintf("%s_%v", path, view.delimiters) + result := templates.GetOrSetFuncLock(key, func() interface{} { + tpl = template.New(key).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap) // Firstly checking the resource manager. if !gres.IsEmpty() { if files := gres.ScanDirFile(path, pattern, true); len(files) > 0 { @@ -219,7 +221,7 @@ func (view *View) getTemplate(path string, pattern string) (tpl *template.Templa } // Secondly checking the file system. - files := ([]string)(nil) + var files []string files, err = gfile.ScanDir(path, pattern, true) if err != nil { return nil @@ -229,8 +231,8 @@ func (view *View) getTemplate(path string, pattern string) (tpl *template.Templa } return tpl }) - if r != nil { - return r.(*template.Template), nil + if result != nil { + return result.(*template.Template), nil } return }