mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
add an example for sessions + view data as requested
This commit is contained in:
42
_examples/sessions/viewdata/main.go
Normal file
42
_examples/sessions/viewdata/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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}}
|
||||
})
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user