mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue in incorrect parsing error message for gview
This commit is contained in:
@ -58,8 +58,12 @@ func (view *View) Parse(file string, params ...Params) (result string, err error
|
||||
var tpl interface{}
|
||||
// It caches the file, folder and its content to enhance performance.
|
||||
r := view.fileCacheMap.GetOrSetFuncLock(file, func() interface{} {
|
||||
var path, folder, content string
|
||||
var resource *gres.File
|
||||
var (
|
||||
path string
|
||||
folder string
|
||||
content string
|
||||
resource *gres.File
|
||||
)
|
||||
// Searching the absolute file path for <file>.
|
||||
path, folder, resource, err = view.searchFile(file)
|
||||
if err != nil {
|
||||
@ -215,15 +219,14 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
// Key for template cache.
|
||||
key := fmt.Sprintf("%s_%v", filePath, view.config.Delimiters)
|
||||
result := templates.GetOrSetFuncLock(key, func() interface{} {
|
||||
// Do not use <key> but the <filePath> as the parameter <name> for function New,
|
||||
// because when error occurs the <name> will be printed out for error locating.
|
||||
tplName := filePath
|
||||
if view.config.AutoEncode {
|
||||
tpl = htmltpl.New(filePath).Delims(
|
||||
tpl = htmltpl.New(tplName).Delims(
|
||||
view.config.Delimiters[0],
|
||||
view.config.Delimiters[1],
|
||||
).Funcs(view.funcMap)
|
||||
} else {
|
||||
tpl = texttpl.New(filePath).Delims(
|
||||
tpl = texttpl.New(tplName).Delims(
|
||||
view.config.Delimiters[0],
|
||||
view.config.Delimiters[1],
|
||||
).Funcs(view.funcMap)
|
||||
@ -232,16 +235,22 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
if !gres.IsEmpty() {
|
||||
if files := gres.ScanDirFile(folderPath, pattern, true); len(files) > 0 {
|
||||
var err error
|
||||
for _, v := range files {
|
||||
if view.config.AutoEncode {
|
||||
_, err = tpl.(*htmltpl.Template).New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
if view.config.AutoEncode {
|
||||
t := tpl.(*htmltpl.Template)
|
||||
for _, v := range files {
|
||||
_, err = t.New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
if err != nil {
|
||||
intlog.Error(err)
|
||||
err = view.formatTemplateObjectCreatingError(v.Name(), tplName, err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
_, err = tpl.(*texttpl.Template).New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
}
|
||||
} else {
|
||||
t := tpl.(*texttpl.Template)
|
||||
for _, v := range files {
|
||||
_, err = t.New(v.FileInfo().Name()).Parse(string(v.Content()))
|
||||
if err != nil {
|
||||
intlog.Error(err)
|
||||
err = view.formatTemplateObjectCreatingError(v.Name(), tplName, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -260,16 +269,16 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
if view.config.AutoEncode {
|
||||
t := tpl.(*htmltpl.Template)
|
||||
for _, file := range files {
|
||||
_, err = t.Parse(gfile.GetContents(file))
|
||||
if err != nil {
|
||||
if _, err = t.Parse(gfile.GetContents(file)); err != nil {
|
||||
err = view.formatTemplateObjectCreatingError(file, tplName, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t := tpl.(*texttpl.Template)
|
||||
for _, file := range files {
|
||||
_, err = t.Parse(gfile.GetContents(file))
|
||||
if err != nil {
|
||||
if _, err = t.Parse(gfile.GetContents(file)); err != nil {
|
||||
err = view.formatTemplateObjectCreatingError(file, tplName, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -282,6 +291,14 @@ func (view *View) getTemplate(filePath, folderPath, pattern string) (tpl interfa
|
||||
return
|
||||
}
|
||||
|
||||
// formatTemplateObjectCreatingError formats the error that creted from creating template object.
|
||||
func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, err error) error {
|
||||
if err != nil {
|
||||
return gerror.NewSkip(1, gstr.Replace(err.Error(), tplName, filePath))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// searchFile returns the found absolute path for <file> and its template folder path.
|
||||
// Note that, the returned <folder> is the template folder path, but not the folder of
|
||||
// the returned template file <path>.
|
||||
|
||||
Reference in New Issue
Block a user