Play Framework config value in view
How can I access the value application.name
开发者_运维技巧 from conf/application.conf
in a view?
You can use following code sample to do so:
${play.configuration['application.name']}
Also see http://groups.google.com/group/play-framework/browse_thread/thread/1412ca8fc3edd22f
Update for Play 2.5.x
In Play Scala 2.5.x, method current in object Play is deprecated. In order to read a value from conf/application.conf, you have to use DI instead.
Inject play.api.Configuration
in your controller :
class MyController @Inject() (val configuration: play.api.Configuration) extends Controller
Then, you can directly use configuration
in your methods :
def sayMyName = Action { request =>
Ok("Your name is " + configuration.getString("application.name"))
}
You can also use configuration
in your view Twirl template :
def sayMyNameUsingView = Action { request =>
implicit lazy val config = configuration
Ok(views.html.index())
}
Send the injected configuration
as implicit to the given index.scala.html
@()(implicit val configuration:play.api.Configuration)
<html>
<body>
<h1>Your name is @configuration.getString("application.name")</h1>
</body>
</html>
Update for Play 2...
In Play Scala 2.3.x, to read a value from conf/application.conf
, you can do the following:
import play.api.Play.current
...
current.configuration.getString("application.name")
精彩评论