How to inject Application in Play Framework 2.5 template

Itchy

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"));
}
costa

I just had to look into this for Play framework 2.6.x. It is possible to inject an object into a template according to the documentation: https://www.playframework.com/documentation/2.6.x/ScalaTemplatesDependencyInjection.

I implemented a simple sample (a bit contrived) and I used scala:

test.scala.html:

@this(configuration: play.api.Configuration)
@(key: String)
config = @configuration.get[Seq[String]](key).mkString(", ")

HomeController.scala

package controllers

import javax.inject._

import play.api._
import play.api.i18n._
import play.api.mvc._

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject()(cc: ControllerComponents, testView: views.html.test) (implicit configuration: Configuration) extends AbstractController(cc) with I18nSupport{

  def test() = Action  { implicit request =>
    Ok(testView("play.i18n.langs"))
  }
}

routes:

GET        /test                controllers.HomeController.test()

The configuration object gets injected into views.html.test template and the view itself is injected into the controller. Note the @this(configuration: play.api.Configuration) statement in the template. Play will generate a class behind the template with a constructor that is injected the Configuration object.

Please note that the injected configuration into the controller doesn't have any role in this particular code. I experimented with other permutations before I found this solution ... Let's say that if you have an inner template used by an outer template called from the controller, and the inner template needs the configuration object you need to feed the configuration from the controller top down, and add an implicit configuration: play.api.Configuration parameter in all the templates in the hierarchy path right to the template that needs it, something like this: @(message: String)(implicit messagesProvider: MessagesProvider, configuration: play.api.Configuration) . Then the controller injected configuration is fed to the top template all the way to the template that needs it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Play Framework Inject Error

From Dev

Play Framework Inject Error

From Dev

How to store a list of Akka actors in Play framework 2 application?

From Dev

How to store a list of Akka actors in Play framework 2 application?

From Dev

Play2 Framework Float Format in Template

From Dev

Play Framework Application and H2-browser

From Dev

Pass reference to a template class in Play2 framework template engine

From Dev

How to tune Play Framework application with proper threadpools?

From Dev

How to deploy a play framework application on ubuntu?

From Dev

Play Framework Scala Template

From Dev

How to build play framework sample application using sbt (not play)?

From Dev

Add <script> to the <head> from scala template tags in Play Framework 2

From Dev

Play Framework 2 template Form None.get

From Dev

play2 framework - dealing with options in a Seq[(String, String)] in a template

From Dev

Automatically inject WebJarAssets in Play 2.5 HTML template?

From Dev

Play framework Application Hosting

From Dev

How to stream a video from Play 2 Framework to HTML5 player

From Dev

How do I change template engine in Play Framework?

From Dev

How do I change template engine in Play Framework?

From Dev

How to get int array with javascript in play framework template?

From Dev

Play Framework: Dependency Inject Action Builder

From Dev

Inject configuration value in play framework using Guice

From Dev

How to reference to the standard ActorSystem of play framework 2?

From Dev

How to combine play framework and angular2?

From Dev

How to install RestFB plugin in Play Framework 2

From Dev

How to "rewrite" urls or routes with Play Framework 2

From Dev

Accessing h2-browser and debugging in play framework application

From Dev

Play 2.x framework Deploy on Heroku application error

From Dev

Display application name dynamically from configuration in Play framework 2

Related Related

  1. 1

    Play Framework Inject Error

  2. 2

    Play Framework Inject Error

  3. 3

    How to store a list of Akka actors in Play framework 2 application?

  4. 4

    How to store a list of Akka actors in Play framework 2 application?

  5. 5

    Play2 Framework Float Format in Template

  6. 6

    Play Framework Application and H2-browser

  7. 7

    Pass reference to a template class in Play2 framework template engine

  8. 8

    How to tune Play Framework application with proper threadpools?

  9. 9

    How to deploy a play framework application on ubuntu?

  10. 10

    Play Framework Scala Template

  11. 11

    How to build play framework sample application using sbt (not play)?

  12. 12

    Add <script> to the <head> from scala template tags in Play Framework 2

  13. 13

    Play Framework 2 template Form None.get

  14. 14

    play2 framework - dealing with options in a Seq[(String, String)] in a template

  15. 15

    Automatically inject WebJarAssets in Play 2.5 HTML template?

  16. 16

    Play framework Application Hosting

  17. 17

    How to stream a video from Play 2 Framework to HTML5 player

  18. 18

    How do I change template engine in Play Framework?

  19. 19

    How do I change template engine in Play Framework?

  20. 20

    How to get int array with javascript in play framework template?

  21. 21

    Play Framework: Dependency Inject Action Builder

  22. 22

    Inject configuration value in play framework using Guice

  23. 23

    How to reference to the standard ActorSystem of play framework 2?

  24. 24

    How to combine play framework and angular2?

  25. 25

    How to install RestFB plugin in Play Framework 2

  26. 26

    How to "rewrite" urls or routes with Play Framework 2

  27. 27

    Accessing h2-browser and debugging in play framework application

  28. 28

    Play 2.x framework Deploy on Heroku application error

  29. 29

    Display application name dynamically from configuration in Play framework 2

HotTag

Archive