-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.go
50 lines (42 loc) · 1.19 KB
/
files.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
package got
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// locate templates in possibly nested subfolders
func findTemplatesRecursively(path string, extension string) (paths []string, err error) {
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err == nil {
if strings.Contains(path, extension) {
paths = append(paths, path)
}
}
return err
})
return
}
// Handles reading templates files in the given directory + ending path
func loadTemplateFiles(dir, path, extension string) (templates map[string][]byte, err error) {
var files []string
files, err = findTemplatesRecursively(filepath.Join(dir, path), extension)
if err != nil {
return
}
templates = make(map[string][]byte)
for _, path = range files {
var b []byte
b, err = ioutil.ReadFile(path)
if err != nil {
return
}
// Convert "templates/layouts/base.html" to "layouts/base"
// For subfolders the extra folder name is included:
// "templates/includes/sidebar/ad1.html" to "includes/sidebar/ad1"
name := strings.TrimPrefix(filepath.Clean(path), filepath.Clean(dir)+"/")
name = strings.TrimSuffix(name, filepath.Ext(name))
templates[name] = b
}
return
}