1package main
2
3import (
4 html "html/template"
5 "io"
6 "os"
7 text "text/template"
8)
9
10type Templater interface {
11 Execute(wr io.Writer, data any) error
12}
13
14var (
15 templateFunc = map[string]any{
16 "DerefI": func(i *int) int { return *i },
17 "DerefS": func(i *string) string { return *i },
18 }
19)
20
21func GetTemplate(templateType, filePath string) (Templater, error) {
22 file, err := os.Open(filePath)
23 if err != nil {
24 return nil, err
25 }
26
27 tmpl, err := io.ReadAll(file)
28 if err != nil {
29 return nil, err
30 }
31
32 switch templateType {
33 case "text":
34 return text.New("text").
35 Funcs(templateFunc).
36 Parse(string(tmpl))
37 case "html":
38 return html.New("html").
39 Funcs(templateFunc).
40 Parse(string(tmpl))
41 default:
42 panic("Invalid template-type")
43 }
44}