1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package render
- import (
- "bytes"
- "gex/pkg/gex"
- "gex/pkg/utils"
- "html/template"
- "path/filepath"
- "strings"
- )
- type Props map[string]any
- type Funcs = template.FuncMap
- func Template(fp string, req *gex.Req, propFunc ...interface{}) string {
- if !strings.Contains(fp, "app/") {
- fp = utils.Cwd(2) + "/" + fp
- }
- fp = strings.Replace(fp, ".tpl", ".gen.tpl", 1)
- var props = Props{}
- var funcs = Funcs{}
- if len(propFunc) > 0 {
- props = propFunc[0].(Props)
- }
- if len(propFunc) > 1 {
- funcs = propFunc[1].(Funcs)
- }
- global(req, props, funcs)
- tmpl, err := template.
- New(filepath.Base(fp)).
- Funcs(funcs).
- ParseFiles(fp)
- if err != nil {
- return err.Error()
- }
- var buf bytes.Buffer
- err = tmpl.Execute(&buf, props)
- if err != nil {
- return err.Error()
- }
- html := buf.String()
- return html
- }
|