-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_func.go
161 lines (153 loc) · 4.1 KB
/
template_func.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package template
import (
"embed"
"fmt"
"io/fs"
"path/filepath"
"strings"
"github.com/nilorg/sdk/path"
)
func loadTemplate(r *Render, templatesDir string, funcMap FuncMap) {
// 加载局部页面
partials, err := filepath.Glob(filepath.Join(templatesDir, "partials/*.tmpl"))
if err != nil {
panic(err)
}
// 加载布局
layouts, err := filepath.Glob(filepath.Join(templatesDir, "layouts/*.tmpl"))
if err != nil {
panic(err)
}
// 加载错误页面
errors, err := filepath.Glob(filepath.Join(templatesDir, "errors/*.tmpl"))
if err != nil {
panic(err)
}
for _, errPage := range errors {
tmplName := fmt.Sprintf("error/%s", filepath.Base(errPage))
files := []string{
errPage,
}
files = append(files, partials...)
r.AddFromFilesFuncs(tmplName, funcMap, files...)
}
// 页面文件夹
var pageDirs []string
basePagePath := filepath.Join(templatesDir, "pages")
err = path.Dirs(basePagePath, &pageDirs)
if err != nil {
panic(err)
}
for _, pageDir := range pageDirs {
for _, layout := range layouts {
pageItems, err := filepath.Glob(filepath.Join(pageDir, "*.tmpl"))
if err != nil {
panic(err)
}
if len(pageItems) == 0 {
continue
}
files := []string{
layout,
}
files = append(files, partials...)
files = append(files, pageItems...)
pageName := pageDir[len(basePagePath)+1:]
tmplName := fmt.Sprintf("%s:pages/%s", filepath.Base(layout), pageName)
r.AddFromFilesFuncs(tmplName, funcMap, files...)
}
}
// 加载单页面
singles, err := filepath.Glob(filepath.Join(templatesDir, "singles/*.tmpl"))
if err != nil {
panic(err)
}
for _, singlePage := range singles {
tmplName := fmt.Sprintf("singles/%s", filepath.Base(singlePage))
files := []string{
singlePage,
}
files = append(files, partials...)
r.AddFromFilesFuncs(tmplName, funcMap, files...)
}
}
// DefaultLoadTemplate ...
func DefaultLoadTemplate(templatesDir string, funcMap FuncMap) Render {
r := NewRender()
loadTemplate(&r, templatesDir, funcMap)
return r
}
func DefaultLoadTemplateWithEmbedFS(tmplFS *embed.FS, tmplFSSUbDir string, funcMap FuncMap) Render {
r := NewRender()
loadTemplateWithEmbedFS(&r, tmplFS, tmplFSSUbDir, funcMap)
return r
}
func loadTemplateWithEmbedFS(r *Render, tmplFS *embed.FS, tmplFSSUbDir string, funcMap FuncMap) {
// 加载局部页面
partials, err := fs.Glob(tmplFS, filepath.Join(tmplFSSUbDir, "partials/*.tmpl"))
if err != nil {
panic(err)
}
// 加载布局
layouts, err := fs.Glob(tmplFS, filepath.Join(tmplFSSUbDir, "layouts/*.tmpl"))
if err != nil {
panic(err)
}
// 加载错误页面
errors, err := fs.Glob(tmplFS, filepath.Join(tmplFSSUbDir, "errors/*.tmpl"))
if err != nil {
panic(err)
}
for _, errPage := range errors {
tmplName := fmt.Sprintf("error/%s", filepath.Base(errPage))
files := []string{
errPage,
}
files = append(files, partials...)
r.AddFromFSFuncs(tmplName, funcMap, tmplFS, files...)
}
// 页面文件夹
var pageDirs []string
basePagePath := filepath.Join(tmplFSSUbDir, "pages")
err = fs.WalkDir(tmplFS, basePagePath, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && path != basePagePath {
pageDirs = append(pageDirs, path)
}
return nil
})
if err != nil {
panic(err)
}
for _, pageDir := range pageDirs {
for _, layout := range layouts {
pageItems, err := fs.Glob(tmplFS, filepath.Join(pageDir, "*.tmpl"))
if err != nil {
panic(err)
}
if len(pageItems) == 0 {
continue
}
files := []string{
layout,
}
files = append(files, partials...)
files = append(files, pageItems...)
pageName := strings.TrimPrefix(pageDir, basePagePath+"/")
tmplName := fmt.Sprintf("%s:pages/%s", filepath.Base(layout), pageName)
r.AddFromFSFuncs(tmplName, funcMap, tmplFS, files...)
}
}
// 加载单页面
singles, err := fs.Glob(tmplFS, filepath.Join(tmplFSSUbDir, "singles/*.tmpl"))
if err != nil {
panic(err)
}
for _, singlePage := range singles {
tmplName := fmt.Sprintf("singles/%s", filepath.Base(singlePage))
files := []string{
singlePage,
}
files = append(files, partials...)
r.AddFromFSFuncs(tmplName, funcMap, tmplFS, files...)
}
}