Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which are the implicit objects available by default on templates?

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?

like image 445
Pere Villega Avatar asked Dec 10 '11 20:12

Pere Villega


People also ask

What are implicit objects?

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.

Which of the following is not implicit object?

E vector and in are not implicit objects.


2 Answers

I've found

  • session which is a play.mvc.Http.Session object
  • request which is a play.mvc.Http.Request object
  • response which is a play.mvc.Http.Response object
  • flash which is a play.mvc.Http.Flash object
  • lang which is a play.i18n.Lang object

are 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...).

like image 52
Brad Mace Avatar answered Nov 16 '22 02:11

Brad Mace


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:

template

// **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"){

}

controller

object Main extends Controller {
  def index = Action {
    // **SECONDLY** declare the request as implicit.
    implicit request =>
      Ok(views.html.index(MyStuff()))
    }
}

More?

Since one single implicit parameter is valid, you could wrap your needed object in a dedicated structure and declare it as implicit.

like image 32
Andy Petrella Avatar answered Nov 16 '22 03:11

Andy Petrella