package main
import (
"errors"
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:
return nil, errors.New("Invalid template type")
}
}