Вопрос

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.

Это было полезно?

Решение

In templates you define vals 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.

Другие советы

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

request.cookies.get("Org").get.value
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top