-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_templates.go
130 lines (118 loc) · 3.69 KB
/
parse_templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gowebly
import (
"fmt"
"html/template"
"os"
"path/filepath"
)
// ParseTemplates parses list of the given templates to the HTTP handler.
//
// Already included 'templates/main.html' layout template from your project
// path.
//
// Example:
//
// import (
// "log/slog"
//
// gowebly "github.com/gowebly/helpers"
// )
//
// func handler(w http.ResponseWriter, r *http.Request) {
// // Define paths to the user templates.
// indexPage := filepath.Join("templates", "pages", "index.html")
// indexLoginForm := filepath.Join("templates", "components", "index-login-form.html")
//
// // Parse user templates or return error.
// tmpl, err := gowebly.ParseTemplates(indexPage, indexLoginForm)
// if err != nil {
// w.WriteHeader(http.StatusBadRequest)
// slog.Error(err.Error(), "method", r.Method, "status", http.StatusBadRequest, "path", r.URL.Path)
// return
// }
//
// // Execute (render) all templates or return error.
// if err := tmpl.Execute(w, nil); err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// slog.Error(err.Error(), "method", r.Method, "status", http.StatusInternalServerError, "path", r.URL.Path)
// return
// }
// }
func ParseTemplates(names ...string) (*template.Template, error) {
// Set global templates.
global := []string{
filepath.Join("templates", "main.html"),
}
// Check if all templates exist.
for _, n := range names {
if !isExistInFolder(n, false) {
return nil, fmt.Errorf("os: template '%s' is not found", n)
}
}
// Add all user templates after global.
global = append(global, names...)
return template.ParseFiles(global...)
}
// ParseTemplatesWithCustomMainLayout parses list of the given templates with a
// custom main layout to the HTTP handler.
//
// Example:
//
// import (
// "log/slog"
//
// gowebly "github.com/gowebly/helpers"
// )
//
// func handler(w http.ResponseWriter, r *http.Request) {
// // Define path to the main layout template.
// customMainLayout := filepath.Join("templates", "my-custom-main.html")
//
// // Define paths to the user templates.
// indexPage := filepath.Join("templates", "pages", "index.html")
// indexLoginForm := filepath.Join("templates", "components", "index-login-form.html")
//
// // Parse user templates or return error.
// tmpl, err := gowebly.ParseTemplatesWithCustomMainLayout(customMainLayout, indexPage, indexLoginForm)
// if err != nil {
// w.WriteHeader(http.StatusBadRequest)
// slog.Error(err.Error(), "method", r.Method, "status", http.StatusBadRequest, "path", r.URL.Path)
// return
// }
//
// // Execute (render) all templates or return error.
// if err := tmpl.Execute(w, nil); err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// slog.Error(err.Error(), "method", r.Method, "status", http.StatusInternalServerError, "path", r.URL.Path)
// return
// }
// }
func ParseTemplatesWithCustomMainLayout(main string, names ...string) (*template.Template, error) {
// Set global templates.
global := make([]string, 0, len(names)+1)
global = append(global, main)
for _, n := range names {
// Check, if the given template is existing.
if !isExistInFolder(n, false) {
return nil, fmt.Errorf("os: template '%s' is not found", n)
}
// Add the given template after global.
global = append(global, n)
}
return template.Must(template.ParseFiles(global...)), nil
}
// isExistInFolder searches for a file or folder by the given name in the
// current folder.
func isExistInFolder(name string, isFolder bool) bool {
// Check if file or folder exists.
_, err := os.Stat(filepath.Clean(name))
if err != nil {
return false
}
// Check if it is a directory.
info, err := os.Lstat(filepath.Clean(name))
if err != nil {
return false
}
return info.IsDir() == isFolder
}