How to pass implicit value from one template layout to another in Play Framework's Twirl?

291 Views Asked by At

let's say I have this controller's Action:

def getPost = Action { implicit request => Ok(views.html.post("bla bla bla")) }

Now I have two layouts:

// baseLayout.scala.html:
@(title: String)(content: Html)(implicit req: RequestHeader)
<html>
  ...
  @content
  ...
</html>

And

// postLayout.scala.html:
@(title: String)(content: Html)(implicit req: RequestHeader)
@baseLayout(title) { // Error: Cannot find any HTTP Request Header here
  <h1>Post</h1>
  @content
}

then I have this template:

// post.scala.html
@(post: String)(implicit req: RequestHeader)
@postLayout("First post") {
  <div>@post</div>
}

This code does not compile. I get this error

Cannot find any HTTP Request Header here (See up)

It is like the implicit RequestHeader is successfully transmitted: from the Action to the post template, then from the post template to postLayout template.

But it is not transmitted from the postLayout template to the baseLayout template.

I am suspecting that it is due to the fact that in postLayout template, the implicit comes after the Html block, it might discard any implicit propagation. Apart from that I don't see any explication, I am completely lost.

0

There are 0 best solutions below