From within a Play 2.1 application, how would I programmatically determine which mode the application is running in (i.e., Development vs. Production)?
For example, it would be useful to be able to do something like this from inside a template:
<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>
In the Play 2.0 API documentation, there appears to be a mode
property of the play.api.Application
class... however, I am unsure about how to get at the instance of the currently running application.
You can access the current Appliction via
play.api.Play.current()
to find out the mode try
play.api.Play.current().mode()
or simply use
play.api.Play.isDev(play.api.Play.current())
In Play 2.5.x the play.Play.isDev()
method is deprecated, one has to use dependency injection:
import javax.inject.Inject; public class Example { @Inject private play.Environment environment; public void myMethod() { if (environment.isDev()) { ... } } }
Or equivalently in Scala:
class ErrorHandler @Inject()(environment: Environment) { def myMethod() = { if (environment.isDev) { ... } } }
environment.isDev()
returns a Boolean, which one can easily pass to a template. No need to use implicit variables as described here.
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