2

Upon getting session information of type map[string]interface{} with this.GetSession("session_key"), I did have to explicitly set the context and type assert the session like this in order to explicitly pass the data to the template.

// 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"

The session data (type map[string]interface{}) looks like this:

{"nickname": "joe", "picture": "urltotheimg"}

However, according to the Beego's session doc, it looks like the session is passed implicitly without any need of type assertions or context passing (the template has immediate access to session values i.e. {{.nickname}} and {{.picture}})

This is the controller setting the session before redirecting to /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)
}

This is the controller of "/user"

type UserController struct {
    beego.Controller
}

func (this *UserController) Get() {
    // get the saved session
    profile := this.GetSession("profile")

    // without setting the template data here, the session data won't be
    // available in user.html
    this.Data["nickname"] = profile.(map[string]interface{})["nickname"].(string)
    this.Data["picture"] = profile.(map[string]interface{})["picture"].(string)
    this.TplNames = "user.html"
}

Only this then I can map the template to the data like this:

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

I'm quite sure it's necessary to set the template data. I'm just not sure why the above doc didn't do that.

Any help would be appreciated.

4

1 回答 1

1

我刚刚尝试运行Beego 快速入门项目并成功运行。

确保你已经安装了beegobee。创建新项目后,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>

和路由器:

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/redirect", &controllers.RedirectController{})
}

我还建议您使用结构来存储配置文件值,例如:

// 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}}

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

于 2015-12-12T06:52:39.933 回答