Teimuraz
Teimuraz

Reputation: 9325

Play framework 2: implicit values in view

I need to pass some variable implicitly in url creation in views.

I have action in controller:

class HomeController extends Controller{

  def index(implicit lang: Lang) = Action.async { implicit request => {
      Future[Result] {
        Ok(views.html.home.index(lang))
      }
    }

    ...
  }

Here is snippet of index.scala.html:

@()(implicit lang: Lang)
@main("Home page") {
     <ul>
        <li><a href="@routes.HomeController.index()"><span>Home</span></a>
     ...
     </ul>
}

But I have compile error at @routes.HomeController.index(), not enough arguments for method

Why is it an error? How can I implicit pass variables in url creation?

Upvotes: 0

Views: 220

Answers (1)

mgosk
mgosk

Reputation: 1874

Try this:

class HomeController extends Controller {
 def index = Action.async { implicit request => {
      Future[Result] {
        Ok(views.html.home.index)
      }
    }
    ...
  }

Do not explictly pass lang param into function . Play Controller contains implict conversion from request to lang, with this lang will be automagically provided into your view.

Upvotes: 1

Related Questions