mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
207842be91
* fix: alway do permission check * fix: alway do permission check * fix: add project query to applications search request * fix: add email template to statik * fix: org members
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package templates
|
|
|
|
import (
|
|
"bytes"
|
|
"html/template"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
templatesPath = "/templates"
|
|
templateFileName = "template.html"
|
|
)
|
|
|
|
func GetParsedTemplate(dir http.FileSystem, contentData interface{}) (string, error) {
|
|
template, err := ParseTemplateFile(dir, "", contentData)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ParseTemplateText(template, contentData)
|
|
}
|
|
|
|
func ParseTemplateFile(dir http.FileSystem, fileName string, data interface{}) (string, error) {
|
|
if fileName == "" {
|
|
fileName = templateFileName
|
|
}
|
|
template, err := readFile(dir, 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
|
|
}
|
|
|
|
func readFile(dir http.FileSystem, fileName string) (*template.Template, error) {
|
|
f, err := dir.Open(templatesPath + "/" + fileName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
content, err := ioutil.ReadAll(f)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tmpl, err := template.New(fileName).Parse(string(content))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tmpl, nil
|
|
}
|