I have the following simplified template setup:
- Main (template)
- Homepage
- Details
Now when a user logs in, a session attribute username
is set so that I can figure out if a user is logged in or not. In order to help me to figure out if a user is logged in or not, I have the following session helper object:
object SessionHelper {
val sessionKey = "username"
def authenticated(implicit request: RequestHeader) = {
request.session.get(sessionKey).exists(_ => true)
}
}
Which I can use in a template that takes an implicit
request object, for example:
(implicit request: play.api.mvc.RequestHeader)
...
@if(SessionHelper.authenticated) {
<strong>Authenticated!</strong>
}
...
Since — as far as I can tell — this is the only way to get an implicit variable in a template, it means that everywhere I render a view, I need to explicitly "define" the implicit request variable. For example:
def index = Action { implicit request =>
Ok(views.html.index(myStuff))
}
Without the implicit request =>
statement, it doesn't compile. Now while this is a little awkward, as long as I stay within any of the "sub-views" (e.g. Homepage or Details) that's fine because for each of those views I have a controller method and as such also access to the implicit RequestHeader
instance. However, when I need to get access to the session in my template (e.g. Main) this doesn't work because it is never explicitly rendered by a controller.
I'm not immediately seeing a way to get access to the session in a template, and a quick Google doesn't reveal much other than a couple questions on the same topic with no resolution. Any ideas?
Update: seems like this is somewhat related to this question.