1 Star 2 Fork 0

haming123/wego

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
web_templates.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
haming 提交于 2022-02-10 15:37 . first commit
package wego
import (
"fmt"
"html/template"
"path/filepath"
"sync"
)
type WebTemplates struct {
mux sync.Mutex
tpl_map map[string]*template.Template
delim_left string
delim_right string
}
func (this *WebTemplates) Init() {
this.tpl_map = make(map[string]*template.Template)
this.delim_left = "{{"
this.delim_right = "}}"
}
func (this *WebTemplates) SetDelim(left, right string){
this.delim_left = left
this.delim_right = right
}
func (this *WebTemplates) getTemplate(filenames ...string) (*template.Template, error) {
this.mux.Lock()
defer this.mux.Unlock()
if len(filenames) == 0 {
return nil, fmt.Errorf("must have one file name")
}
key := ""
for _, item := range filenames {
key += item
}
tpl, ok := this.tpl_map[key]
if ok {
//fmt.Println("use template cache")
return tpl, nil
}
//fmt.Println("load template")
name := filepath.Base(filenames[0])
t, err := template.New(name).Delims(this.delim_left, this.delim_right).Funcs(tmplFuncMap).ParseFiles(filenames...)
if err == nil {
this.tpl_map[key] = t
}
return t, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/haming123/wego.git
git@gitee.com:haming123/wego.git
haming123
wego
wego
main

搜索帮助

A270a887 8829481 3d7a4017 8829481