master
Evan Chen 2021-11-04 12:00:31 +08:00
parent 33e556899b
commit 77be1d6ae3
3 changed files with 105 additions and 1 deletions

102
engine/template.go Normal file
View File

@ -0,0 +1,102 @@
package engine
import (
"bytes"
"html/template"
"io"
"io/fs"
"path/filepath"
)
type Engine struct {
name string
Template *template.Template
}
func New(name string) *Engine {
Ename := "E" + name
if Ename == "E" {
Ename = "Engine"
}
return &Engine{Ename, template.New(name)}
}
func (e *Engine) Funcs(funcMap template.FuncMap) *Engine {
e.Template = e.Template.Funcs(funcMap)
return e
}
func (e *Engine) Parse(text string) (*Engine, error) {
tmp, err := e.Template.Parse(text)
if err != nil {
return e, err
}
e.Template = tmp
return e, err
}
func (e *Engine) ParseFS(fs fs.FS, patterns ...string) (*Engine, error) {
tmp, err := e.Template.ParseFS(fs, patterns...)
if err != nil {
return e, err
}
e.Template = tmp
return e, err
}
func (e *Engine) ParseFiles(filenames ...string) (*Engine, error) {
tmp, err := e.Template.ParseFiles(filenames...)
if err != nil {
return e, err
}
e.Template = tmp
return e, err
}
func (e *Engine) ParseGlob(pattern string) (*Engine, error) {
tmp, err := e.Template.ParseGlob(pattern)
if err != nil {
return e, err
}
e.Template = tmp
return e, err
}
func (e *Engine) OverrideGlob(pattern string) (*Engine, error) {
matches, _ := filepath.Glob(pattern)
if len(matches) <= 0 {
// surpass error if no tmpl found
return e, nil
}
tmp, err := e.Template.ParseGlob(pattern)
if err != nil {
return e, err
}
e.Template = tmp
return e, err
}
func Must(e *Engine, err error) *Engine {
if err != nil {
panic(err)
}
return e
}
func (e *Engine) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
buf := &bytes.Buffer{}
err := e.Template.ExecuteTemplate(buf, name, data)
if err != nil {
return err
}
_, err = wr.Write(buf.Bytes())
return err
}
func (e *Engine) Execute(wr io.Writer, data interface{}) error {
buf := &bytes.Buffer{}
err := e.Template.Execute(buf, data)
if err != nil {
return err
}
_, err = wr.Write(buf.Bytes())
return err
}

2
go.mod
View File

@ -2,7 +2,7 @@ module kumoly.io/lib/ksrv
go 1.17 go 1.17
require kumoly.io/lib/klog v0.1.9 require kumoly.io/lib/klog v0.1.10
require ( require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect

2
go.sum
View File

@ -6,3 +6,5 @@ kumoly.io/lib/klog v0.1.8 h1:CguIVyo2T/E2cdrc30mFh/KPhIiM/K2hNV8aR8KuSdw=
kumoly.io/lib/klog v0.1.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= kumoly.io/lib/klog v0.1.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.1.9 h1:rS9PPqfyBIIfeQlPSuMv+7StGPiFVuAdp04HDwwDY3E= kumoly.io/lib/klog v0.1.9 h1:rS9PPqfyBIIfeQlPSuMv+7StGPiFVuAdp04HDwwDY3E=
kumoly.io/lib/klog v0.1.9/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= kumoly.io/lib/klog v0.1.9/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.1.10 h1:GJxwcsUct8nF3oHtsJPTIlPKTUeB/+7jsbgh3bMvKMc=
kumoly.io/lib/klog v0.1.10/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=