iris/_examples/sessions/viewdata/main.go
2020-11-02 18:46:38 +02:00

43 lines
966 B
Go

package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
)
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
sess := sessions.New(sessions.Config{Cookie: "session_cookie", AllowReclaim: true})
app.Use(sess.Handler())
// ^ use app.UseRouter instead to access sessions on HTTP errors too.
// Register our custom middleware, after the sessions middleware.
app.Use(setSessionViewData)
app.Get("/", index)
app.Listen(":8080")
}
func setSessionViewData(ctx iris.Context) {
session := sessions.Get(ctx)
ctx.ViewData("session", session)
ctx.Next()
}
func index(ctx iris.Context) {
session := sessions.Get(ctx)
session.Set("username", "kataras")
ctx.View("index")
/* OR without middleware:
ctx.View("index", iris.Map{
"session": session,
// {{.session.Get "username"}}
// OR to pass only the 'username':
// "username": session.Get("username"),
// {{.username}}
})
*/
}