mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gview
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame 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,
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/ghash"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"github.com/gogf/gf/os/gfsnotify"
|
||||
"github.com/gogf/gf/os/gmlock"
|
||||
@ -32,7 +33,7 @@ import (
|
||||
|
||||
const (
|
||||
// Template name for content parsing.
|
||||
gCONTENT_TEMPLATE_NAME = "TemplateContent"
|
||||
templateNameForContentParsing = "TemplateContent"
|
||||
)
|
||||
|
||||
// fileCacheItem is the cache item for template file.
|
||||
@ -106,6 +107,9 @@ func (view *View) Parse(file string, params ...Params) (result string, err error
|
||||
} else {
|
||||
tpl, err = tpl.(*texttpl.Template).Parse(item.content)
|
||||
}
|
||||
if err != nil && item.path != "" {
|
||||
err = gerror.Wrap(err, item.path)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -151,15 +155,15 @@ func (view *View) ParseContent(content string, params ...Params) (string, error)
|
||||
return "", nil
|
||||
}
|
||||
err := (error)(nil)
|
||||
key := fmt.Sprintf("%s_%v_%v", gCONTENT_TEMPLATE_NAME, view.config.Delimiters, view.config.AutoEncode)
|
||||
key := fmt.Sprintf("%s_%v_%v", templateNameForContentParsing, view.config.Delimiters, view.config.AutoEncode)
|
||||
tpl := templates.GetOrSetFuncLock(key, func() interface{} {
|
||||
if view.config.AutoEncode {
|
||||
return htmltpl.New(gCONTENT_TEMPLATE_NAME).Delims(
|
||||
return htmltpl.New(templateNameForContentParsing).Delims(
|
||||
view.config.Delimiters[0],
|
||||
view.config.Delimiters[1],
|
||||
).Funcs(view.funcMap)
|
||||
}
|
||||
return texttpl.New(gCONTENT_TEMPLATE_NAME).Delims(
|
||||
return texttpl.New(templateNameForContentParsing).Delims(
|
||||
view.config.Delimiters[0],
|
||||
view.config.Delimiters[1],
|
||||
).Funcs(view.funcMap)
|
||||
@ -232,12 +236,12 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
if view.config.AutoEncode {
|
||||
_, err = tpl.(*htmltpl.Template).New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
intlog.Error(err)
|
||||
}
|
||||
} else {
|
||||
_, err = tpl.(*texttpl.Template).New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
intlog.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -246,18 +250,28 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
}
|
||||
|
||||
// Secondly checking the file system.
|
||||
var files []string
|
||||
var (
|
||||
files []string
|
||||
)
|
||||
files, err = gfile.ScanDir(folderPath, pattern, true)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if view.config.AutoEncode {
|
||||
if tpl, err = tpl.(*htmltpl.Template).ParseFiles(files...); err != nil {
|
||||
return nil
|
||||
t := tpl.(*htmltpl.Template)
|
||||
for _, file := range files {
|
||||
_, err = t.Parse(gfile.GetContents(file))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if tpl, err = tpl.(*texttpl.Template).ParseFiles(files...); err != nil {
|
||||
return nil
|
||||
t := tpl.(*texttpl.Template)
|
||||
for _, file := range files {
|
||||
_, err = t.Parse(gfile.GetContents(file))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return tpl
|
||||
|
||||
Reference in New Issue
Block a user