浏览代码

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

Manuel Reithuber 4 年之前
父节点
当前提交
88b10da596
共有 3 个文件被更改,包括 35 次插入21 次删除
  1. 15 14
      httpd/webadmin.go
  2. 4 4
      httpd/webclient.go
  3. 16 3
      utils/utils.go

+ 15 - 14
httpd/webadmin.go

@@ -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

+ 4 - 4
httpd/webclient.go

@@ -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

+ 16 - 3
utils/utils.go

@@ -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)