mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-18 22:07:32 +00:00
46 lines
984 B
Go
46 lines
984 B
Go
|
package templates
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"html/template"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
templateFileName = "template.html"
|
||
|
)
|
||
|
|
||
|
func GetParsedTemplate(contentData interface{}) (string, error) {
|
||
|
template, err := ParseTemplateFile("", contentData)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return ParseTemplateText(template, contentData)
|
||
|
}
|
||
|
|
||
|
func ParseTemplateFile(fileName string, data interface{}) (string, error) {
|
||
|
if fileName == "" {
|
||
|
fileName = templateFileName
|
||
|
}
|
||
|
template, err := template.ParseFiles(fileName)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return parseTemplate(template, data)
|
||
|
}
|
||
|
|
||
|
func ParseTemplateText(text string, data interface{}) (string, error) {
|
||
|
template, err := template.New("template").Parse(text)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return parseTemplate(template, data)
|
||
|
}
|
||
|
|
||
|
func parseTemplate(template *template.Template, data interface{}) (string, error) {
|
||
|
buf := new(bytes.Buffer)
|
||
|
if err := template.Execute(buf, data); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return buf.String(), nil
|
||
|
}
|