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?
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:
Configuration
in controllerSend 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With