feat: update auto render
This commit is contained in:
99
debug/debug_render.go
Normal file
99
debug/debug_render.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 模拟渲染器的数据结构
|
||||
tableName := "categories"
|
||||
|
||||
// 模拟模板数据
|
||||
templateData := map[string]interface{}{
|
||||
"Table": tableName,
|
||||
"TableAlias": "分类",
|
||||
"Columns": []interface{}{map[string]interface{}{"Name": "id", "Alias": "ID", "ShowInList": true}},
|
||||
"Data": []interface{}{map[string]interface{}{"id": 1, "name": "测试"}},
|
||||
"Total": 1,
|
||||
"Page": 1,
|
||||
"PerPage": 10,
|
||||
"Pages": 1,
|
||||
"Search": "",
|
||||
"SortField": "",
|
||||
"SortOrder": "asc",
|
||||
"Tables": []interface{}{map[string]interface{}{"Alias": "categories"}},
|
||||
"CurrentPath": "/",
|
||||
"StartRecord": 1,
|
||||
"EndRecord": 1,
|
||||
"LastUpdate": "2025-08-07",
|
||||
}
|
||||
|
||||
// 加载并测试模板
|
||||
builtinPath, _ := filepath.Abs("./web/templates/builtin")
|
||||
|
||||
// 基础模板函数
|
||||
funcs := template.FuncMap{
|
||||
"dict": func(values ...interface{}) map[string]interface{} {
|
||||
dict := make(map[string]interface{})
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
if i+1 < len(values) {
|
||||
key := fmt.Sprintf("%v", values[i])
|
||||
dict[key] = values[i+1]
|
||||
}
|
||||
}
|
||||
return dict
|
||||
},
|
||||
"add": func(a, b int) int { return a + b },
|
||||
"sub": func(a, b int) int { return a - b },
|
||||
"json": func(v interface{}) string {
|
||||
return fmt.Sprintf("%v", v)
|
||||
},
|
||||
}
|
||||
|
||||
// 加载布局模板
|
||||
layoutPath := filepath.Join(builtinPath, "layout.html")
|
||||
listPath := filepath.Join(builtinPath, "list.html")
|
||||
|
||||
// 解析模板
|
||||
tmpl := template.New("test").Funcs(funcs)
|
||||
|
||||
// 读取并解析布局
|
||||
layoutContent, err := os.ReadFile(layoutPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading layout: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取并解析列表模板
|
||||
listContent, err := os.ReadFile(listPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading list: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析模板内容
|
||||
tmpl, err = tmpl.Parse(string(layoutContent))
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing layout: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err = tmpl.Parse(string(listContent))
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing list: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 测试渲染
|
||||
fmt.Println("Testing template render...")
|
||||
err = tmpl.ExecuteTemplate(os.Stdout, "layout", templateData)
|
||||
if err != nil {
|
||||
fmt.Printf("Error executing template: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("\nTemplate render test completed successfully!")
|
||||
}
|
||||
59
debug/debug_template.go
Normal file
59
debug/debug_template.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rogeecn/database_render/internal/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Test template loading
|
||||
builtinPath, _ := filepath.Abs("./web/templates/builtin")
|
||||
customPath, _ := filepath.Abs("./web/templates/custom")
|
||||
|
||||
fmt.Printf("Builtin path: %s\n", builtinPath)
|
||||
fmt.Printf("Custom path: %s\n", customPath)
|
||||
|
||||
// Check if paths exist
|
||||
if _, err := os.Stat(builtinPath); os.IsNotExist(err) {
|
||||
fmt.Printf("Builtin path does not exist: %s\n", builtinPath)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(customPath); os.IsNotExist(err) {
|
||||
fmt.Printf("Custom path does not exist: %s\n", customPath)
|
||||
return
|
||||
}
|
||||
|
||||
// Test loading list template for categories
|
||||
loader := template.NewTemplateLoader(template.TemplateConfig{
|
||||
BuiltinPath: builtinPath,
|
||||
CustomPath: customPath,
|
||||
CacheEnabled: false,
|
||||
})
|
||||
|
||||
tmpl, err := loader.LoadTemplate("list", "categories")
|
||||
if err != nil {
|
||||
fmt.Printf("Error loading template: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Template loaded successfully: %v\n", tmpl != nil)
|
||||
|
||||
// Test file existence
|
||||
builtinList := filepath.Join(builtinPath, "list.html")
|
||||
if _, err := os.Stat(builtinList); err != nil {
|
||||
fmt.Printf("Builtin list.html not found: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("Builtin list.html found: %s\n", builtinList)
|
||||
}
|
||||
|
||||
layoutFile := filepath.Join(builtinPath, "layout.html")
|
||||
if _, err := os.Stat(layoutFile); err != nil {
|
||||
fmt.Printf("Layout.html not found: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("Layout.html found: %s\n", layoutFile)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user