apkdoc @ 08dcb18a941a80a3b37c14c8d64dd3a229e86ab1

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