gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:49 26 4
gpt4 key购买 nike

我目前正在使用 iris web 框架,由于无法在问题跟踪器上提出问题,而且社区聊天已经停止,所以我在这里提出这个问题,希望有人能帮助我。

我需要将数据传递给 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函数中添加一些默认数据呢?

最佳答案

// 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("已记录", false)

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

编码愉快!

关于Golang(iris webframework)在处理程序之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com