Golang(iris webframework)在处理程序之间共享

时间:2016-09-10 23:33:57

标签: go

我目前正在使用虹膜网络框架,因为无法在问题跟踪器上询问问题,社区聊天已经死了我在这里问这个希望有人帮助我。

我需要将数据传递给c.Render函数

我有一个处理程序,用于检查用户是否已登录。如果没有记录,我应该在html页面添加一个额外的按钮

iris.Use(userHandler{})

type userHandler struct{
    Allow bool
}

func (u userHandler) Serve(c *iris.Context) {
    ...
    if isLogged {
        // When I call from another middleware (c.Next) c.Render it should know that the user is logged in
    }
    c.Next()
}

那么可以在c.Render函数中添加一些默认数据吗?

1 个答案:

答案 0 :(得分:1)

// retrieve local storage or previous handler,
// this is how handlers can share values, with the context's Values().
logged := ctx.Values().Get("logged")

// set template data {{.isLogged}}
ctx.ViewData("isLogged", logged)

// and finally, render the mypage.html
ctx.View("mypage.html")

"logged"可以通过以下方式设置为您的中间件/任何以前的处理程序:

ctx.Values().Set("logged", false)

所有这些都在示例中进行了描述,欢迎您在那里探讨其中的一些内容:https://github.com/kataras/iris/tree/master/_examples

快乐的编码!