4

I'm new to PlayFramework.

Please give me a sample how to access Configuration parameters from my view. I'm using PlayFramework 2.5.3

Old way (@current is deprecated):

@current.configuration.getString("environment.param")

New way (as far as I understand, Configuration should be injected):

I know how to access it from controller.

@Inject() (val messagesApi: MessagesApi, configuration: Configuration)

How do I use it from my view?

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
Mike Pakhomov
  • 425
  • 6
  • 14
  • If you need this only in one or two places, you can provide the configuration as a parameter to your view. – Anton Sarov Apr 30 '16 at 13:09
  • DI in twirl templates will come soon hopefully: https://github.com/playframework/twirl/pull/100 – adis May 01 '16 at 19:18

3 Answers3

9

Sadly there isn't much you can do about it. It is just how it is when DI was introduced to Play, not much was discussed in regard to templates. One possible solution could be:

  1. Inject Configuration in controller
  2. Send it as implicit to your view/template

    class Application @Inject() (implicit val config: Configuration) extends Controller {
    
        def index = Action {
            Ok(views.html.index("foo"))
        }
    }
    

And your template will look like:

@(myParam1: Any)(implicit config: Configuration)
<h2>Some HTML here @myParam1 @config.getString("environment.param")</h2>

I am totally aware that this somehow defeats the purpose of DI but this is just how it is right now.

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
  • Thanks! It works. My solution was the same as yours, but I didn't add implicit lazy val config = configuration. And that's why I got errors. – Mike Pakhomov Apr 30 '16 at 13:41
  • 1
    You could change this to `class Application @Inject() (implicit val config: Configuration) ..` and it would have the same effect, then you could remove the redundant `implicit lazy val config = configuration` – Michael Zajac Apr 30 '16 at 13:53
  • can you explain how does it defeat the purpose of DI? – Łukasz Apr 30 '16 at 15:38
  • 1
    Corrected the answer as suggested by m-z, indeed a cleaner solution. @Łukasz well with parameters you actually **have to** provide the configuration to the template - which is not what is DI about. In DI we want the template to say *"Give me the configuration"* instead of providing it – Anton Sarov Apr 30 '16 at 15:51
  • Isn't DI about that object does not create its dependencies, but they are created outside and delivered through for example constructor. The template says give me the configuration by saying, to call me you need to pass in configuration. Same goes for controller, it gets the dependency through a constructor. No matter if Guice injects it, or you inject it in the code it's still DI. At least this is how I understand DI. – Łukasz Apr 30 '16 at 16:13
  • Well yeah you could argue that as long "someone" injects something, then it is DI. But from my point of view DI is when Guice (or similar) takes care of injecting the needed dependency - so that I only specify what I need but never have to provide it myself. – Anton Sarov Apr 30 '16 at 17:15
  • Are you sure that you want whole config in view ? Maybe you can pass single variable from config. – mgosk Apr 30 '16 at 20:35
  • Sure, you can do this, it is just that the OP did not specify this precisely. But nothing stops you from extracting the needed information in the controller and pass only that to the view. – Anton Sarov May 01 '16 at 07:19
2

Looks like there might be changes in 2.6.x or twirl 1.2.0

https://github.com/playframework/twirl/pull/100

https://www.playframework.com/documentation/2.6.x/ScalaTemplates#Template-constructor

Dino Fancellu
  • 1,974
  • 24
  • 33
0

In java, to read from application.conf in play 2.5, you should inject configuration in your controller as below:

public class HomeController extends Controller {
private Configuration configuration;

@Inject
public HomeController(Configuration configuration) {
    this.configuration = configuration;
}

public Result index() {
    String value = configuration.getString("key");
    System.out.println("value of key is " + key);
    return ok(value);
}

}

Because Configuration is a concrete class, binding it in Module class is not needed.

see also this discussion: DI for java in play 2.5

Community
  • 1
  • 1