With the new Version 2.4/2.5 of the Play Framework they moved further towards injecting everything and removing the servers state. play.Play.application()
is now deprecated. However, I need the application in my template (e.g. to get all supported languages displayed on all pages with play.i18n.Lang.availables(play.Play.application())
).
I'm aware I could:
- Pass
play.Application
explicitly to all of my templates. - Add an implicit parameter to my template like
@()(implicit app: play.Application)
. However, in my Java-Project it's not really implicit, I have to pass it every time I render the template. - Create a Scala object providing the application implicitly. However, this also needs the deprecated
play.api.Play.current
.
How can I inject play.Application
in my templates?
---- Update: ----
What I've tried so far, I created the following setup:
index.scala.html:
@(title: String)
@template(title) { //here is the play.Application obviously missing, however I don't want to pass it in every template - even worse in every Controller <-- this is the core of my question
Welcome to my page!
}
template.scala.html:
@(title: String)(content: Html)(implicit app: play.Application)
<html>
<head>
<title>@title</title>
</head>
<body>
<p>Is live? @app.isProd</p>
@content
</body>
</html>
Controller function:
public Result index() {
return ok(views.html.index.render("home"));
}
new play.api.i18n.DefaultLangs(play.Play.application().configuration()).availables()
to show a language-switcher. – Itchy