I'm trying to create a plugin for Play Framework 2.0 (latest code in Github as of today, 10th December 2011). The plugin must render some data in the view. To avoid forcing users to modify all the templates, I created a tag that will retrieve and render the relevant data.
To avoid concurrency issues that data should be stored along an identifier for the user. This identifier only needs to be valid during a request (session id, request itself, etc), after the request ends the information would be discarded.
The issue is that neither Request, RequestHeader nor Session are available as implicit values inside a Scala template. So, which are the implicit values available in a template?
If there are none I can use, do you have any suggestion on how to obtain a value that I can access from templates and controllers (without requiring a user to modify neither the controller or the template) and that can be generated in a per-request basis?
Implicit objects are a set of Java objects that the JSP Container makes available to developers on each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.JSP provide you Total 9 implicit objects which are as follows.
E vector and in are not implicit objects.
I've found
session
which is a play.mvc.Http.Session objectrequest
which is a play.mvc.Http.Request objectresponse
which is a play.mvc.Http.Response objectflash
which is a play.mvc.Http.Flash objectlang
which is a play.i18n.Lang objectare still available, while the implicit objects errors
, out
, params
, and play
from 1.x have been removed. It looks like this list is determined by play.mvc.Http.Context.Implicit, whose javadoc says "Import in templates to get implicit HTTP context". So I'm fairly confident this is the entire list of what's available automatically.
There is also a @Messages
thing which seems to replace the messages
implicit object along with the &{'my.message.key'}
syntax from 1.x. It's actually shorthand for a method invocation rather than being an implicit object. You can do @Messages("my.message.key")
which invokes play.Messages.get(String,Object...), or you can force a certain language with things like @Messages("index.title")(Lang("es"))
or @Messages("index.title")(Lang("es-ar"))
which invoke play.Messages.get(Lang,String,Object...).
Yes it was a problem to me too...
Actually, they could have one implicit
but still that you have to add it to the parameters list and to declare the needed object as implicit
in the Action.
Here is how I did:
// **FIRST** add the needed implicit parameter to the template (the whole template will be created as a function will the related parameters, incl. curryed parameters list are handled)
@(stuff:models.MyStuff)(implicit request: play.api.mvc.Request[Any])
@import helper._
@main("Home"){
}
object Main extends Controller {
def index = Action {
// **SECONDLY** declare the request as implicit.
implicit request =>
Ok(views.html.index(MyStuff()))
}
}
Since one single implicit
parameter is valid, you could wrap your needed object in a dedicated structure and declare it as implicit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With