mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
增加模板对象变量示例程序
This commit is contained in:
parent
17acd8df5a
commit
7d79ab5690
2
TODO.MD
2
TODO.MD
@ -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);
|
||||
|
||||
|
||||
|
||||
|
28
geg/os/gview/object/object.go
Normal file
28
geg/os/gview/object/object.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
@ -1 +0,0 @@
|
||||
123456789
|
Loading…
Reference in New Issue
Block a user