scalaplayframework-2.3

Play Framework - Getting Session Data in Views


How can I get session data in views in Play Framework 2.3.x?

I used to use session.get("sessionName") but now I get not found: value session when I try to use that.


Solution

  • You ought to pass all mandatory variables to a view. In order to get access to the session you should declare it as parameter of your template.

    @(title:String)(implicit session: Session)
    
    <html>
        <head>
            <title>@title</title>
        </head>
        <body>
            @session.get("sessionname")
        </body>
    </html>
    

    Using implicit will allow you to avoid passing the session explicitly from a controller.

    def index = Action { implicit req =>
      Ok(views.html.index("Page Title"))
    }