updated utils.LoadTemplate() to call template.ParseFiles() directly and added a way to specify a base template (will be used in the next commit)

This commit is contained in:
Manuel Reithuber 2021-06-05 18:06:53 +02:00 committed by Nicola Murino
parent c07dc74d48
commit 88b10da596
3 changed files with 35 additions and 21 deletions

View file

@ -251,20 +251,21 @@ func loadAdminTemplates(templatesPath string) {
setupPath := []string{
filepath.Join(templatesPath, templateAdminDir, templateSetup),
}
usersTmpl := utils.LoadTemplate(template.ParseFiles(usersPaths...))
userTmpl := utils.LoadTemplate(template.ParseFiles(userPaths...))
adminsTmpl := utils.LoadTemplate(template.ParseFiles(adminsPaths...))
adminTmpl := utils.LoadTemplate(template.ParseFiles(adminPaths...))
connectionsTmpl := utils.LoadTemplate(template.ParseFiles(connectionsPaths...))
messageTmpl := utils.LoadTemplate(template.ParseFiles(messagePath...))
foldersTmpl := utils.LoadTemplate(template.ParseFiles(foldersPath...))
folderTmpl := utils.LoadTemplate(template.ParseFiles(folderPath...))
statusTmpl := utils.LoadTemplate(template.ParseFiles(statusPath...))
loginTmpl := utils.LoadTemplate(template.ParseFiles(loginPath...))
changePwdTmpl := utils.LoadTemplate(template.ParseFiles(changePwdPaths...))
maintenanceTmpl := utils.LoadTemplate(template.ParseFiles(maintenancePath...))
defenderTmpl := utils.LoadTemplate(template.ParseFiles(defenderPath...))
setupTmpl := utils.LoadTemplate(template.ParseFiles(setupPath...))
usersTmpl := utils.LoadTemplate(nil, usersPaths...)
userTmpl := utils.LoadTemplate(nil, userPaths...)
adminsTmpl := utils.LoadTemplate(nil, adminsPaths...)
adminTmpl := utils.LoadTemplate(nil, adminPaths...)
connectionsTmpl := utils.LoadTemplate(nil, connectionsPaths...)
messageTmpl := utils.LoadTemplate(nil, messagePath...)
foldersTmpl := utils.LoadTemplate(nil, foldersPath...)
folderTmpl := utils.LoadTemplate(nil, folderPath...)
statusTmpl := utils.LoadTemplate(nil, statusPath...)
loginTmpl := utils.LoadTemplate(nil, loginPath...)
changePwdTmpl := utils.LoadTemplate(nil, changePwdPaths...)
maintenanceTmpl := utils.LoadTemplate(nil, maintenancePath...)
defenderTmpl := utils.LoadTemplate(nil, defenderPath...)
setupTmpl := utils.LoadTemplate(nil, setupPath...)
adminTemplates[templateUsers] = usersTmpl
adminTemplates[templateUser] = userTmpl

View file

@ -123,10 +123,10 @@ func loadClientTemplates(templatesPath string) {
filepath.Join(templatesPath, templateClientDir, templateClientMessage),
}
filesTmpl := utils.LoadTemplate(template.ParseFiles(filesPaths...))
credentialsTmpl := utils.LoadTemplate(template.ParseFiles(credentialsPaths...))
loginTmpl := utils.LoadTemplate(template.ParseFiles(loginPath...))
messageTmpl := utils.LoadTemplate(template.ParseFiles(messagePath...))
filesTmpl := utils.LoadTemplate(nil, filesPaths...)
credentialsTmpl := utils.LoadTemplate(nil, credentialsPaths...)
loginTmpl := utils.LoadTemplate(nil, loginPath...)
messageTmpl := utils.LoadTemplate(nil, messagePath...)
clientTemplates[templateClientFiles] = filesTmpl
clientTemplates[templateClientCredentials] = credentialsTmpl

View file

@ -353,9 +353,22 @@ func CleanPath(p string) string {
return path.Clean(p)
}
// LoadTemplate wraps a call to a function returning (*Template, error)
// it is just like template.Must but it writes a log before exiting
func LoadTemplate(t *template.Template, err error) *template.Template {
// LoadTemplate parses the given template paths.
// it behaves like template.Must but it writes a log before exiting
// you can optionally provide a base template (e.g. to define some custom functions)
func LoadTemplate(base *template.Template, paths ...string) *template.Template {
var t *template.Template
var err error
if base != nil {
t, err = base.ParseFiles(paths...)
if err == nil {
t, err = t.Clone()
}
} else {
t, err = template.ParseFiles(paths...)
}
if err != nil {
logger.ErrorToConsole("error loading required template: %v", err)
logger.Error(logSender, "", "error loading required template: %v", err)