Play 20 : implicit object in views

538 views
Skip to first unread message

zka

unread,
Nov 18, 2011, 6:18:05 AM11/18/11
to play-framework
Hello,

is it possible to pass implicit objects in views ? like request or
flash ?

in Play 1.x whe had renderArgs.

Drew Hamlett

unread,
Nov 18, 2011, 9:10:45 AM11/18/11
to play-framework
It's done like this now.

@(users : Seq[User])(implicit flash: Flash)

Put this at the top of your view.

In Play 1.x, request, session and a few other objects were always
available in the view.

Brian Nesbitt

unread,
Nov 18, 2011, 9:27:50 AM11/18/11
to play-fr...@googlegroups.com
https://github.com/playframework/Play20/wiki/JavaTemplates

Search for "And even implicit parameters" about 1/2 way down the page

@(title: String)(body: => Html)(implicit request: play.api.mvc.Request)

flurdy

unread,
Feb 23, 2012, 8:10:46 PM2/23/12
to play-fr...@googlegroups.com
(I know I am replying to an older thread but it seemed relevant)
Does this mean if I require an implicit object in the core layout template, I then need to add the implicit parameter to it and subsequently every template? And then also add the object to every controller method? It seems that way, but not very DRY.

I am trying to exclude external content in development mode, such as analytics, adsense, twitter calls etc In Play 1.x this was easily done in one place only with either "#{ifnot request.domain=='localhost'}" or "#{ifnot play.Play.mode==play.Play.Mode.DEV}"

Manuel Bernhardt

unread,
Feb 24, 2012, 2:02:51 AM2/24/12
to play-fr...@googlegroups.com
Hi,

You can add an import to an object in your SBT build definition like e.g.

templatesImport += "views.Implicits._"

Then the implicits from there are in scope in all templates

Manuel

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/cSOXBuMm4SwJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

flurdy

unread,
Feb 25, 2012, 8:18:30 AM2/25/12
to play-fr...@googlegroups.com
Nice. Knew there would be a way :)


On Friday, February 24, 2012 7:02:51 AM UTC, Manuel Bernhardt wrote:
Hi,

You can add an import to an object in your SBT build definition like e.g.

    templatesImport += "views.Implicits._"

Then the implicits from there are in scope in all templates

Manuel

sas

unread,
Feb 25, 2012, 2:46:34 PM2/25/12
to play-framework
Hey!

nice tip

should go straight to the docs

Andriy Zhdanov

unread,
Feb 25, 2012, 3:42:07 PM2/25/12
to play-framework
Yeah, nice tip, but I think the question was for this, e.g.:

object Application extends Controller {
implicit def username(implicit request: RequestHeader) =
request.session.get("username").orElse(
request.cookies.get("rememberme").map( _.value ))
}

then your view can use it as request or flash:

@()(implicit flash: Flash, request: RequestHeader, user:
Option[String])

Thank you.

Steven Wong

unread,
Aug 31, 2012, 6:06:28 AM8/31/12
to play-fr...@googlegroups.com
Hi,

Where is views.Implicits? I've tried adding both views.Implicits and views.implicits to my Build.scala and both says

implicits is not a member of package views
import views.Implicits._
             ^

Steve
Reply all
Reply to author
Forward
0 new messages