内置变量

内置变量

Config

仅在Web Server下,即通过ghttp模块使用ghttp.Response/gmvc.View对象渲染模板引擎时有效。

访问默认的配置管理(config.toml)对象Map值。

使用方式:

`.Config.配置项路径`

仅在Web Server下,即通过ghttp模块使用ghttp.Response/gmvc.View对象渲染模板引擎时有效。

访问当前请求的Cookie对象Map值。

使用方式:

`.Cookie.键名`

Session

仅在Web Server下,即通过ghttp模块使用ghttp.Response/gmvc.View对象渲染模板引擎时有效。

访问当前请求的Session对象Map值。

使用方式:

`.Session.键名`

使用示例:

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()
}

其中,config.toml内容为:

## Redis数据库配置
[redis]
    disk  = "127.0.0.1:6379,0"
    cache = "127.0.0.1:6379,1"

执行后,访问http://127.0.0.1:8199/,输出结果为:

Config:127.0.0.1:6379,1, Cookie:default, Session:john