mirror of
https://gitee.com/gokins/gokins.git
synced 2024-12-02 03:07:46 +08:00
html string++
This commit is contained in:
parent
2e651ef86a
commit
11def2eff0
@ -4,4 +4,6 @@ import "github.com/gin-gonic/gin"
|
||||
|
||||
var (
|
||||
Gin *gin.Engine
|
||||
|
||||
FileView = false
|
||||
)
|
||||
|
18
main.go
18
main.go
@ -8,13 +8,6 @@ import (
|
||||
"html/template"
|
||||
)
|
||||
|
||||
var testHTML = `
|
||||
<html>
|
||||
<head><title>{{AppName}}-测试</title></head>
|
||||
<body>内容:{{.cont}}</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
func main() {
|
||||
comm.Gin = gin.Default()
|
||||
comm.Gin.FuncMap = template.FuncMap{
|
||||
@ -22,10 +15,17 @@ func main() {
|
||||
return "mine app"
|
||||
},
|
||||
}
|
||||
comm.Gin.Any("/ping", func(c *gin.Context) {
|
||||
//comm.FileView=true
|
||||
comm.Gin.LoadHTMLGlob("view/*")
|
||||
comm.Gin.Any("/test", func(c *gin.Context) {
|
||||
data := ruisUtil.NewMap()
|
||||
data.Set("cont", "你好啊world!")
|
||||
utils.RenderHTML(c, testHTML, data)
|
||||
utils.Render(c, "test.html", data)
|
||||
})
|
||||
|
||||
err := utils.InitHtmls()
|
||||
if err != nil {
|
||||
println("InitHtmls err:" + err.Error())
|
||||
}
|
||||
comm.Gin.Run(":8050")
|
||||
}
|
||||
|
@ -4,12 +4,11 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/render"
|
||||
"gokins/comm"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
func RenderHTML(c *gin.Context, htmls string, data interface{}) {
|
||||
t := template.New("test").Funcs(comm.Gin.FuncMap)
|
||||
/*tpe:=reflect.TypeOf(c).Elem()
|
||||
func RenderHTML(c *gin.Context, name string, data interface{}) {
|
||||
/*t := template.New("test").Funcs(comm.Gin.FuncMap)
|
||||
tpe:=reflect.TypeOf(c).Elem()
|
||||
vae:=reflect.ValueOf(c).Elem()
|
||||
_,ok:=tpe.FieldByName("engine")
|
||||
if ok{
|
||||
@ -17,16 +16,24 @@ func RenderHTML(c *gin.Context, htmls string, data interface{}) {
|
||||
eg:=(*gin.Engine)(unsafe.Pointer(val))
|
||||
//println("val:",eg)
|
||||
t.Funcs(eg.FuncMap)
|
||||
}*/
|
||||
}
|
||||
_, err := t.Parse(htmls)
|
||||
if err != nil {
|
||||
c.String(500, "errs:"+err.Error())
|
||||
return
|
||||
}
|
||||
}*/
|
||||
|
||||
c.Render(200, render.HTML{
|
||||
Template: t,
|
||||
Name: "",
|
||||
Template: HtmlSource,
|
||||
Name: name,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
func Render(c *gin.Context, name string, data interface{}) {
|
||||
if comm.FileView {
|
||||
c.HTML(200, name, data)
|
||||
} else {
|
||||
RenderHTML(c, name, data)
|
||||
}
|
||||
}
|
||||
|
40
utils/htmls.go
Normal file
40
utils/htmls.go
Normal file
@ -0,0 +1,40 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"gokins/comm"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
var HtmlSource = template.New("go")
|
||||
|
||||
func InitHtmls() error {
|
||||
HtmlSource.Funcs(comm.Gin.FuncMap)
|
||||
_, err := HtmlSource.New("head.html").Parse(`
|
||||
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||
<meta name="viewport" content="initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,width=device-width"/>
|
||||
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = HtmlSource.New("test.html").Parse(`
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{AppName}}-测试</title>
|
||||
{{template "head.html" .}}
|
||||
</head>
|
||||
<body>
|
||||
go 内容:{{.cont}}
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
3
view/head.html
Normal file
3
view/head.html
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||
<meta name="viewport" content="initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,width=device-width"/>
|
11
view/test.html
Normal file
11
view/test.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{AppName}}-测试</title>
|
||||
{{template "head.html" .}}
|
||||
</head>
|
||||
<body>
|
||||
view 内容:{{.cont}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user