Take the 2-minute tour ×

I need to override implicited values befor it pass to the template, but dont have idea how. Something like this:

/* This Session User and City setup */

case class MySession(user: Option[User] = None, city: Option[City] = None) {}

/* Trait for Controllers */

trait CMySession {
    implicit def mySession[A](implicit request: Request[A]) : MySession = {
        val userOpt = /*... get from session user here ...*/
        val cityOpt = /*... get from session city here ...*/
        MySession(user = userOpt, city = cityOpt)

/* Controller */

def showCity(city_name: String) = Action { implicit request =>
    // Get city
    val cityOpt = { for (c <- mySession.city) yield Some(c) } getOrElse Cities.getByName(city_name)
    // Check if NO City in session, but we get it from request
    if (mySession.city != cityOpt) {
       // NEED some how override implicited mySession value here for template?!

Thank you for any clues!

share|improve this question
see Action composition in the documentation. You can wrap Request and inject your own properties and access via template with say, "implicit r: controllers.MyWrappedRequest[_]" as a method param. "@r.foo.user.map(...)" –  virtualeyes Jan 26 '13 at 13:45

1 Answer 1

up vote 2 down vote accepted

The great thing about implicit values in Scala is that you can override them, either by declaring your own implicit value in scope (in your case in the if block) or by passing it explicitly (in your case the template, e.g. views.html.showCity(session = myOtherSession)).

share|improve this answer

Your Answer


