-
Notifications
You must be signed in to change notification settings - Fork 3
/
templates.go
43 lines (34 loc) · 835 Bytes
/
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
package main
import (
"embed"
"fmt"
"strings"
"text/template"
)
//go:embed templates
var templateFS embed.FS
func GetStandardTemplate(templateName string) (*template.Template, error) {
fileName := fmt.Sprintf("templates/%s.tmpl", templateName)
rawTmpl, readErr := templateFS.ReadFile(fileName)
if readErr != nil {
return nil, readErr
}
tmpl, parseErr := template.New(templateName).Parse(string(rawTmpl))
if parseErr != nil {
return nil, parseErr
}
return tmpl, nil
}
func GetStandardTemplates() []string {
files, readErr := templateFS.ReadDir("templates")
if readErr != nil {
panic(readErr)
}
var templateNames []string
for _, file := range files {
if strings.HasSuffix(file.Name(), ".tmpl") {
templateNames = append(templateNames, file.Name()[0:len(file.Name())-5])
}
}
return templateNames
}