Skip to content

Instantly share code, notes, and snippets.

@julienrf
Last active October 1, 2015 04:18
Show Gist options
  • Save julienrf/1918776 to your computer and use it in GitHub Desktop.
Save julienrf/1918776 to your computer and use it in GitHub Desktop.
Composite UI without boilerplate using Play 2
object Application extends Controller {
val index = Action { implicit request =>
Ok(views.html.index())
}
val show = Action { implicit request =>
Ok(views.html.show("42"))
}
implicit def user(implicit session: Session): Option[User] = {
for (name <- session.get("username")) yield User(name)
}
}
object Application extends Controller {
val index = Action { implicit request =>
Ok(views.html.index(user(session)))
}
val show = Action { implicit request =>
Ok(views.html.show(user(session), "42"))
}
def user(session: Session): Option[User] = {
for (name <- session.get("username")) yield User(name)
}
}
@(user: Option[User], message: String)
@main(user) {
<h1>@message</h1>
}
@(message: String)(implicit user: Option[User])
@main {
<h1>@message</h1>
}
trait Users {
implicit def user(implicit session: Session): Option[User] = {
for (name <- session.get("username")) yield User(name)
}
}
object Application extends Controller with Users {
val index = Action { implicit request =>
Ok(views.html.index())
}
val show = Action { implicit request =>
Ok(views.html.show("42"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment