gf/os/gview/gview_doparse.go

304 lines
8.6 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gview
import (
"bytes"
"errors"
"fmt"
2019-10-31 23:37:33 +08:00
"github.com/gogf/gf/encoding/ghash"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gfcache"
"github.com/gogf/gf/os/gfsnotify"
"github.com/gogf/gf/os/gmlock"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"strconv"
2019-08-16 00:29:14 +08:00
"strings"
"text/template"
2019-07-29 21:01:19 +08:00
2019-08-16 00:29:14 +08:00
"github.com/gogf/gf/os/gres"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gspath"
)
2019-06-05 21:58:27 +08:00
const (
// Template name for content parsing.
gCONTENT_TEMPLATE_NAME = "template content"
)
var (
// Templates cache map for template folder.
2019-06-11 20:57:43 +08:00
// TODO Note that there's no expiring logic for this map.
templates = gmap.NewStrAnyMap(true)
resourceTryFiles = []string{"template/", "template", "/template", "/template/"}
)
2019-10-31 23:37:33 +08:00
// ParseContent parses given template file <file>
// with given template parameters <params> and function map <funcMap>
// and returns the parsed string content.
func (view *View) Parse(file string, params ...Params) (result string, err error) {
view.mu.RLock()
defer view.mu.RUnlock()
var tpl *template.Template
// It here uses map cache to enhance the performance.
r := view.fileCacheMap.GetOrSetFuncLock(file, func() interface{} {
var path, folder string
var resource *gres.File
// Searching the absolute file path for <file>.
path, folder, resource, err = view.searchFile(file)
if err != nil {
return nil
}
2019-10-31 23:37:33 +08:00
// Get the template object instance for <folder>.
tpl, err = view.getTemplate(folder, fmt.Sprintf(`*%s`, gfile.Ext(path)))
if err != nil {
return nil
}
2019-10-31 23:37:33 +08:00
// Using memory lock to ensure concurrent safety for template parsing.
gmlock.LockFunc("gview.Parse:"+folder, func() {
if resource != nil {
tpl, err = tpl.Parse(gconv.UnsafeBytesToStr(resource.Content()))
} else {
tpl, err = tpl.Parse(gfcache.GetContents(path))
}
2019-08-16 00:29:14 +08:00
})
2019-10-31 23:37:33 +08:00
if err != nil {
return nil
}
2019-10-31 23:37:33 +08:00
// Monitor template files changes using fsnotify asynchronously.
if resource == nil {
if _, err := gfsnotify.AddOnce("gview.Parse:"+folder, folder, func(event *gfsnotify.Event) {
// CLEAR THEM ALL.
view.fileCacheMap.Clear()
templates.Clear()
gfsnotify.Exit()
}); err != nil {
intlog.Error(err)
}
2019-08-16 00:29:14 +08:00
}
2019-10-31 23:37:33 +08:00
return tpl
})
2019-10-31 23:37:33 +08:00
if r == nil {
return
}
2019-10-31 23:37:33 +08:00
tpl = r.(*template.Template)
// Note that the template variable assignment cannot change the value
// of the existing <params> or view.data because both variables are pointers.
2019-10-31 23:37:33 +08:00
// It needs to merge the values of the two maps into a new map.
var variables map[string]interface{}
length := len(view.data)
if len(params) > 0 {
length += len(params[0])
}
if length > 0 {
variables = make(map[string]interface{}, length)
}
if len(view.data) > 0 {
if len(params) > 0 {
2019-10-31 23:37:33 +08:00
if variables == nil {
variables = make(map[string]interface{})
}
for k, v := range params[0] {
variables[k] = v
}
for k, v := range view.data {
variables[k] = v
}
} else {
variables = view.data
}
} else {
if len(params) > 0 {
variables = params[0]
}
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, variables); err != nil {
return "", err
}
2019-10-31 23:37:33 +08:00
// TODO any graceful plan to replace "<no value>"?
result = gstr.Replace(buffer.String(), "<no value>", "")
result = view.i18nTranslate(result, variables)
2019-08-31 18:04:12 +08:00
return result, nil
}
// ParseContent parses given template content <content>
// with given template parameters <params> and function map <funcMap>
// and returns the parsed content in []byte.
2019-06-19 09:06:52 +08:00
func (view *View) ParseContent(content string, params ...Params) (string, error) {
view.mu.RLock()
defer view.mu.RUnlock()
2019-06-05 21:58:27 +08:00
err := (error)(nil)
2019-06-19 09:06:52 +08:00
tpl := templates.GetOrSetFuncLock(gCONTENT_TEMPLATE_NAME, func() interface{} {
2019-06-05 21:58:27 +08:00
return template.New(gCONTENT_TEMPLATE_NAME).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap)
}).(*template.Template)
// Using memory lock to ensure concurrent safety for content parsing.
2019-10-31 23:37:33 +08:00
hash := strconv.FormatUint(ghash.DJBHash64([]byte(content)), 10)
gmlock.LockFunc("gview.ParseContent:"+hash, func() {
2019-06-05 21:58:27 +08:00
tpl, err = tpl.Parse(content)
})
if err != nil {
return "", err
}
// Note that the template variable assignment cannot change the value
// of the existing <params> or view.data because both variables are pointers.
2019-10-31 23:37:33 +08:00
// It needs to merge the values of the two maps into a new map.
var variables map[string]interface{}
length := len(view.data)
if len(params) > 0 {
length += len(params[0])
}
if length > 0 {
variables = make(map[string]interface{}, length)
}
if len(view.data) > 0 {
if len(params) > 0 {
2019-10-31 23:37:33 +08:00
if variables == nil {
variables = make(map[string]interface{})
}
for k, v := range params[0] {
variables[k] = v
}
for k, v := range view.data {
variables[k] = v
}
} else {
variables = view.data
}
} else {
if len(params) > 0 {
variables = params[0]
}
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, variables); err != nil {
return "", err
}
2019-10-31 23:37:33 +08:00
// TODO any graceful plan to replace "<no value>"?
2019-08-31 18:04:12 +08:00
result := gstr.Replace(buffer.String(), "<no value>", "")
result = view.i18nTranslate(result, variables)
2019-08-31 18:04:12 +08:00
return result, nil
}
2019-10-31 23:37:33 +08:00
// getTemplate returns the template object associated with given template folder <path>.
// It uses template cache to enhance performance, that is, it will return the same template object
// with the same given <path>. It will also refresh the template cache
// if the template files under <path> 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)
// Firstly checking the resource manager.
if !gres.IsEmpty() {
if files := gres.ScanDirFile(path, pattern, true); len(files) > 0 {
var err error
for _, v := range files {
_, err = tpl.New(v.FileInfo().Name()).Parse(string(v.Content()))
if err != nil {
glog.Error(err)
}
}
return tpl
}
}
// Secondly checking the file system.
files := ([]string)(nil)
files, err = gfile.ScanDir(path, pattern, true)
if err != nil {
return nil
}
if tpl, err = tpl.ParseFiles(files...); err != nil {
return nil
}
return tpl
})
if r != nil {
return r.(*template.Template), nil
}
return
}
// searchFile returns the found absolute path for <file>, and its template folder path.
func (view *View) searchFile(file string) (path string, folder string, resource *gres.File, err error) {
// Firstly checking the resource manager.
if !gres.IsEmpty() {
for _, v := range resourceTryFiles {
if resource = gres.Get(v + file); resource != nil {
path = resource.Name()
folder = v
return
}
}
view.paths.RLockFunc(func(array []string) {
for _, v := range array {
v = strings.TrimRight(v, "/"+gfile.Separator)
if resource = gres.Get(v + "/" + file); resource != nil {
path = resource.Name()
folder = v
break
}
if resource = gres.Get(v + "/template/" + file); resource != nil {
path = resource.Name()
folder = v + "/template"
break
}
}
})
}
// Secondly checking the file system.
if path == "" {
view.paths.RLockFunc(func(array []string) {
for _, v := range array {
v = strings.TrimRight(v, gfile.Separator)
if path, _ = gspath.Search(v, file); path != "" {
folder = v
break
}
if path, _ = gspath.Search(v+gfile.Separator+"template", file); path != "" {
folder = v + gfile.Separator + "template"
break
}
}
})
}
// Error checking.
if path == "" {
buffer := bytes.NewBuffer(nil)
if view.paths.Len() > 0 {
buffer.WriteString(fmt.Sprintf("[gview] cannot find template file \"%s\" in following paths:", file))
view.paths.RLockFunc(func(array []string) {
index := 1
for _, v := range array {
v = strings.TrimRight(v, "/")
if v == "" {
v = "/"
}
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v))
index++
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, strings.TrimRight(v, "/")+gfile.Separator+"template"))
index++
}
})
} else {
buffer.WriteString(fmt.Sprintf("[gview] cannot find template file \"%s\" with no path set/add", file))
}
if errorPrint() {
glog.Error(buffer.String())
}
err = errors.New(fmt.Sprintf(`template file "%s" not found`, file))
}
return
}