wp-go/app/theme/templateFuncs.go

47 lines
984 B
Go
Raw Permalink Normal View History

package theme
2023-01-17 15:18:31 +00:00
import (
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-01-17 15:18:31 +00:00
"html/template"
"time"
)
2023-01-21 06:49:24 +00:00
var comFn = template.FuncMap{
2023-01-17 15:18:31 +00:00
"unescaped": func(s string) any {
return template.HTML(s)
},
"dateCh": func(t time.Time) any {
return t.Format("2006年 01月 02日")
},
2023-03-06 15:43:58 +00:00
"timeFormat": func(t time.Time, format string) any {
return t.Format(format)
},
2023-01-17 15:18:31 +00:00
"getOption": func(k string) string {
return wpconfig.GetOption(k)
2023-01-17 15:18:31 +00:00
},
2023-01-19 06:12:59 +00:00
"getLang": wpconfig.GetLang,
2023-04-15 16:57:50 +00:00
"postsFn": postsFn,
2023-04-24 13:51:43 +00:00
"exec": func(fn func() string) template.HTML {
return template.HTML(fn())
2023-04-15 16:57:50 +00:00
},
2024-01-21 13:29:36 +00:00
"callFuncString": func(fn func(string) string, s string) template.HTML {
return template.HTML(fn(s))
},
2023-04-15 16:57:50 +00:00
}
func postsFn(fn func(models.Posts) string, a models.Posts) string {
return fn(a)
2023-01-17 15:18:31 +00:00
}
func FuncMap() template.FuncMap {
2023-01-21 06:49:24 +00:00
return comFn
2023-01-17 15:18:31 +00:00
}
2023-01-26 17:03:41 +00:00
func addTemplateFunc(fnName string, fn any) {
2023-01-21 06:49:24 +00:00
if _, ok := comFn[fnName]; ok {
panic("exists same name func")
2023-01-17 15:18:31 +00:00
}
2023-01-21 06:49:24 +00:00
comFn[fnName] = fn
2023-01-17 15:18:31 +00:00
}