mirror of
https://gitee.com/johng/gf
synced 2026-07-01 03:21:22 +08:00
增加模板引擎内置变量使用示例
This commit is contained in:
18
geg/net/ghttp/server/template/buildin-vars.go
Normal file
18
geg/net/ghttp/server/template/buildin-vars.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Cookie.Set("theme", "default")
|
||||
r.Session.Set("name", "john")
|
||||
content :=`Config:{{.Config.redis.cache}}, Cookie:{{.Cookie.theme}}, Session:{{.Session.name}}`
|
||||
r.Response.WriteTplContent(content, nil)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
4
geg/net/ghttp/server/template/config.toml
Normal file
4
geg/net/ghttp/server/template/config.toml
Normal file
@ -0,0 +1,4 @@
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
disk = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
@ -8,14 +8,7 @@ import (
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
content :=
|
||||
`
|
||||
{{if (get "name")}}
|
||||
{{get "name"}}
|
||||
{{else}}
|
||||
NoName
|
||||
{{end}}
|
||||
`
|
||||
content :=`{{if (get "name")}} {{get "name"}} {{else}} NoName {{end}}`
|
||||
r.Response.WriteTplContent(content, nil)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
Reference in New Issue
Block a user