Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Session Beego会话未自动向模板传递数据_Session_Go_Beego - Fatal编程技术网

Session Beego会话未自动向模板传递数据

Session Beego会话未自动向模板传递数据,session,go,beego,Session,Go,Beego,在使用this.GetSession(“session_key”)获取类型为map[string]interface{}的会话信息时,我必须像这样显式地设置上下文和类型断言会话,以便显式地将数据传递给模板 // Get the session profile := this.GetSession("profile") // Have to add data to the template's context this.Data["nickname"] = profile.(map[string

在使用
this.GetSession(“session_key”)
获取类型为
map[string]interface{}
的会话信息时,我必须像这样显式地设置上下文和类型断言会话,以便显式地将数据传递给模板

// Get the session
profile := this.GetSession("profile")

// Have to add data to the template's context
this.Data["nickname"] = profile.(map[string]interface{})["nickname"].(string)
this.Data["picture"] = profile.(map[string]interface{})["picture"].(string)

// Render template
this.TplNames = "user.html"
会话数据(类型
map[string]接口{}
)如下所示:

{"nickname": "joe", "picture": "urltotheimg"}
<img src="{{ .picture }}">
<p>Hello, {{ .nickname }}</p>
但是,根据Beego的会话,会话似乎是隐式传递的,不需要任何类型断言或上下文传递(模板可以立即访问会话值,即
{{.nickname}}
{.picture}

这是控制器在重定向到
/user

// Inherit beego's base controller
type MyController struct {
    beego.Controller
}

func (this *MyController) Get() {

    // code for getting token here

    // Getting the User information
    client := conf.Client(oauth2.NoContext, token)
    resp, err := client.Get("https://" + domain + "/userinfo")
    if err != nil {
        this.Redirect("/error", 500)
        return
    }

    // Reading the body for user's information
    raw, err := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()
    if err != nil {
        this.Redirect("/error", 500)
        return
    }

    // Unmarshalling the JSON of the Profile
    var profile map[string]interface{}
    if err := json.Unmarshal(raw, &profile); err != nil {
        this.Redirect("/error", 500)
        return
    }

    // Saving the information to the session.
    this.SetSession("profile", profile)

    // redirect to /user
    this.Redirect("/user", 301)
}
这是“/用户”的控制器

只有这样,我才能将模板映射到如下数据:

{"nickname": "joe", "picture": "urltotheimg"}
<img src="{{ .picture }}">
<p>Hello, {{ .nickname }}</p>

你好,{{.昵称}

我很确定有必要设置模板数据。我只是不知道为什么上面的医生没有这么做

任何帮助都将不胜感激。

我刚刚尝试运行该项目并成功运行

确保已安装和。使用
bee new projectname
创建新项目后,确保编辑projectname/conf/app.conf文件并添加sessionon=true

appname = quickstart
httpport = 8080
runmode = dev
sessionon = true
我创建了一个重定向控制器,如:

type RedirectController struct {
    beego.Controller
}

func (c *RedirectController) Get() {
    profile := make(map[string]interface{})
    profile["nickname"] = "User's Nickname"
    profile["picture"] = "/path/to/img.jpg"

    c.SetSession("profile", profile)
    c.Redirect("/", 301)
}
主控制器:

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    profile := c.GetSession("profile")

    c.Data["nickname"] = profile.(map[string]interface{})["nickname"]
    c.Data["picture"] = profile.(map[string]interface{})["picture"]
    c.TplNames = "index.tpl"
}
我的index.tpl文件:

<p>Nickname: {{.nickname}}</p>
<p>Picture: {{.picture}}</p>
我还建议您使用结构来存储配置文件值,如:

// Define struct.
type Profile struct{
    Nickname string
    Picture  string
}

// Save it for template rendering.
this.Data["profile"] = &Profile{Nickname:"astaxie", Picture:"img.jpg"}

// And render it like this:
Nickname: {{.profile.Nickname}}
Picture:  {{.profile.Picture}}

确保阅读以了解模板渲染是如何完成的。我希望这是您的要求,如果不是,请编辑您的问题并添加更多有用的信息,我将编辑此答案。

对不起,您的问题是什么?我不明白是否需要键入assert the profile session并分配给Data map以引用模板上下文中的数据。我看到的几个示例只是获取会话并呈现模板,数据在上下文中可用。@pie-o-pah我在读取您希望在模板上显示的内容时遇到了问题。模板上呈现的是什么?因为我看到您的
缺少“.”。此外,您正在从Auth0Controller读取这些值。您确定这些值设置正确吗?如果您只是将UserController中的值硬编码为:
this.Data[“昵称”]=“用户昵称”
this.Data[“picture”]=“/path/to/img.jpg,会发生什么“
@pie-o-pah另外,你能补充一下你的问题吗?你是如何配置begoo的以及你的项目的文件和文件夹结构是什么样子的?谢谢,我完全没有注意到Beego总是需要用
这个来设置模板数据。数据[]
而在vanilla Go中,数据可以传递到
RenderTemplate
方法中。不过,谢谢你指出!