代码拉取完成,页面将自动刷新
package view
import (
"bytes"
"gitee.com/zhucheer/orange/utils"
"html/template"
htmlTemplate "html/template"
"net/url"
"path/filepath"
textTemplate "text/template"
)
var htmlTemplateFuncs = template.FuncMap{
"unescaped": unescaped,
"urlencode": urlencode,
"urldecode": urldecode,
}
// TextPath 渲染文本
func TextPath(tplPath string, viewData interface{}, includeFiles ...string) (res string) {
return textPath(tplPath, viewData, includeFiles...)
}
// textPath 渲染文本类型模版
func textPath(tplPath string, viewData interface{}, includeFiles ...string) (res string) {
tplName := filepath.Base(tplPath)
showBuffer := bytes.NewBuffer([]byte{})
tmpl := textTemplate.Must(textTemplate.New(tplName).ParseFiles(tplPath))
tmpl.ParseFiles(includeFiles...)
tmpl.Execute(showBuffer, viewData)
res = showBuffer.String()
return
}
// TextContent 根据模板内容渲染文本类型模版
func TextContent(tplContent string, viewData interface{}, includeFiles ...string) (res string) {
tplName := "ORANGE-TPL" + utils.ShortTag(tplContent, 0)
showBuffer := bytes.NewBuffer([]byte{})
tmpl := textTemplate.Must(textTemplate.New(tplName).Parse(tplContent))
tmpl.ParseFiles(includeFiles...)
tmpl.Execute(showBuffer, viewData)
res = showBuffer.String()
return
}
// htmlPath 渲染html类型模版
func htmlPath(tplPath string, viewData interface{}, includeFiles ...string) (res string) {
tplName := filepath.Base(tplPath)
showBuffer := bytes.NewBuffer([]byte{})
tmpl := htmlTemplate.Must(htmlTemplate.New(tplName).Funcs(htmlTemplateFuncs).ParseFiles(tplPath))
tmpl.ParseFiles(includeFiles...)
tmpl.Execute(showBuffer, viewData)
res = showBuffer.String()
return
}
// unescaped 模版函数
func unescaped(x string) interface{} {
return template.HTML(x)
}
// urlencode
func urlencode(x string) interface{} {
return template.URLQueryEscaper(x)
}
// urldecode
func urldecode(x string) interface{} {
decode, _ := url.PathUnescape(x)
return decode
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。