2025-12-26 22:35:18 +08:00
|
|
|
|
package tmpl
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-27 02:50:16 +08:00
|
|
|
|
"encoding/json"
|
2025-12-27 21:59:58 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"html/template"
|
|
|
|
|
|
"io/fs"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
2025-12-27 02:50:16 +08:00
|
|
|
|
"strings"
|
2025-12-26 22:35:18 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func New(app *gin.Engine) {
|
|
|
|
|
|
app.Static("/assets", "./res/assets")
|
2025-12-27 02:50:16 +08:00
|
|
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
|
|
"mod": GetRemainder,
|
|
|
|
|
|
"add": GetAdd,
|
|
|
|
|
|
"div": GetDivision,
|
|
|
|
|
|
"sub": Sub,
|
|
|
|
|
|
"seq": Seq,
|
|
|
|
|
|
"mul": GetMultiply,
|
|
|
|
|
|
"test": TestFunc,
|
|
|
|
|
|
}
|
|
|
|
|
|
app.SetFuncMap(funcMap)
|
|
|
|
|
|
|
2025-12-27 21:59:58 +08:00
|
|
|
|
// 加载所有HTML模板
|
|
|
|
|
|
templ := loadTemplates("./res/templates")
|
|
|
|
|
|
app.SetHTMLTemplate(templ)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// loadTemplates 加载所有HTML模板
|
|
|
|
|
|
func loadTemplates(templatesDir string) *template.Template {
|
|
|
|
|
|
tmpl := template.New("")
|
|
|
|
|
|
|
|
|
|
|
|
// 首先加载组件和布局模板(.tpl文件)
|
|
|
|
|
|
err := filepath.Walk(templatesDir, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 加载.tpl文件(组件和布局)
|
|
|
|
|
|
if !info.IsDir() && strings.HasSuffix(path, ".html") {
|
|
|
|
|
|
content, err := os.ReadFile(path)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 使用相对路径作为模板名,保持目录结构
|
|
|
|
|
|
relPath, _ := filepath.Rel(templatesDir, path)
|
|
|
|
|
|
templateName := strings.ReplaceAll(relPath, "\\", "/")
|
|
|
|
|
|
|
|
|
|
|
|
// 如果不是 components 或 layouts 目录中的文件,且包含 {{define "content"}}
|
|
|
|
|
|
// 则将其重命名为唯一的 content 名称(基于文件名)
|
|
|
|
|
|
contentStr := string(content)
|
|
|
|
|
|
_, err = tmpl.New(templateName).Parse(contentStr)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("解析模板 %s 失败: %v", path, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Printf("已加载组件/布局模板: %s\n", templateName)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("加载组件模板失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tmpl
|
2025-12-27 02:50:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestFunc(in string) string {
|
|
|
|
|
|
return strings.ToUpper(in)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func AnyToBytes(data any) ([]byte, error) {
|
|
|
|
|
|
return json.Marshal(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义取余函数
|
|
|
|
|
|
func GetRemainder(a, b int) int {
|
|
|
|
|
|
return a % b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 整数相加
|
|
|
|
|
|
func GetAdd(a, b int) int {
|
|
|
|
|
|
return a + b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 整数相乘
|
|
|
|
|
|
func GetMultiply(a, b int) int {
|
|
|
|
|
|
return a * b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 整数相除
|
|
|
|
|
|
func GetDivision(a, b int) int {
|
|
|
|
|
|
if b == 0 {
|
|
|
|
|
|
return 0 // 当除数为 0 时返回 0,避免 panic
|
|
|
|
|
|
}
|
|
|
|
|
|
return a / b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 整数相减
|
|
|
|
|
|
func Sub(a, b int) int {
|
|
|
|
|
|
return a - b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 获取页码
|
|
|
|
|
|
func Seq(start, end int) []int {
|
|
|
|
|
|
result := make([]int, end-start+1)
|
|
|
|
|
|
for i := range result {
|
|
|
|
|
|
result[i] = start + i
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
2025-12-26 22:35:18 +08:00
|
|
|
|
}
|