增加模板对象变量示例程序

This commit is contained in:
John 2018-11-03 15:59:24 +08:00
parent 17acd8df5a
commit 7d79ab5690
4 changed files with 43 additions and 19 deletions

View File

@ -40,7 +40,7 @@
- https://github.com/namreg/godown
- https://github.com/Masterminds/sprig
1. gform参考 https://gohouse.github.io/gorose/dist/index.html 进行改进
1. 模板引擎增加对象的支持(参考https://segmentfault.com/q/1010000016829214)
1. 模板引擎增加对象的支持(参考https://segmentfault.com/q/1010000016829214)

View File

@ -0,0 +1,28 @@
package main
import (
"gitee.com/johng/gf/g"
)
type T struct {
Name string
}
func (t *T) Hello(name string) string {
return "Hello " + name
}
func (t *T) Test() string {
return "This is test"
}
func main() {
t := &T{"John"}
v := g.View()
content := `{{.t.Hello "there"}}, my name's {{.t.Name}}. {{.t.Test}}.`
if r, err := v.ParseContent(content, g.Map{"t" : t}); err != nil {
g.Dump(err)
} else {
g.Dump(r)
}
}

View File

@ -1,24 +1,21 @@
package main
import (
"gitee.com/johng/gf/g/frame/gmvc"
"gitee.com/johng/gf/g/net/ghttp"
"html/template"
"log"
"os"
)
type ControllerIndex struct {
gmvc.Controller
}
func (c *ControllerIndex) Info() {
c.View.Assign("title", "Go Frame 第一个网站")
c.View.Assigns(map[string]interface{}{
"name" : "很开心",
"score" : 100,
})
c.View.Display("web/index.html")
type Person string
func (p Person) Label() string {
return "This is " + string(p)
}
func main() {
s := ghttp.GetServer()
s.SetServerRoot("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/other/web/")
s.BindController("/", new(ControllerIndex))
s.SetPort(8199)
s.Run()
tmpl, err := template.New("").Parse(`{{sum 1 2 3}}`)
if err != nil {
log.Fatalf("Parse: %v", err)
}
tmpl.Execute(os.Stdout, nil)
}

View File

@ -1 +0,0 @@
123456789