Reputation: 20252
I am trying to read a cookie value inside the play framework template (not inside a controller). I am trying the following which is not working:
@ val cookieVal = request.cookies.get('PLAY_SESSION').value
Any suggestions to fix this will be greatly appreciated. The reason why I am trying this is to change how the page gets rendered based on a cookie value.
Upvotes: 3
Views: 1427
Reputation: 15490
suppose PLAY_SESSION stored "37f0983881ba00636868b42234a360d466fb944c-block_status=0&userId=159313257462171"
and you have to render on the basis of the value of block_status
.
then in this case you can get its value by
session.get("block_status").get
to use it in template you have to import@implicit session:play.api.mvc.Session
at your template.
now you can easily get values at template by @session.get("block_status").get
Upvotes: 1
Reputation:
In templates you define val
s as follows:
@defining(request.cookies.get('PLAY_SESSION').value) { theValue =>
<div>Hello @theValue</div>
}
I personally prefer to read the cookies in the controller and pass them to the template if needed.
Upvotes: 3