aboutsummaryrefslogtreecommitdiff
path: root/template.go
diff options
context:
space:
mode:
Diffstat (limited to 'template.go')
-rw-r--r--template.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/template.go b/template.go
new file mode 100644
index 0000000..503af64
--- /dev/null
+++ b/template.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ html "html/template"
+ "io"
+ "os"
+ text "text/template"
+)
+
+type Templater interface {
+ Execute(wr io.Writer, data any) error
+}
+
+var (
+ templateFunc = map[string]any{
+ "DerefI": func(i *int) int { return *i },
+ "DerefS": func(i *string) string { return *i },
+ }
+)
+
+func GetTemplate(templateType, filePath string) (Templater, error) {
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, err
+ }
+
+ tmpl, err := io.ReadAll(file)
+ if err != nil {
+ return nil, err
+ }
+
+ switch templateType {
+ case "text":
+ return text.New("text").
+ Funcs(templateFunc).
+ Parse(string(tmpl))
+ case "html":
+ return html.New("html").
+ Funcs(templateFunc).
+ Parse(string(tmpl))
+ default:
+ panic("Invalid template-type")
+ }
+}