0
votes

I have a link to open a website in one of the views in my application and I need for that website to be dependant on a site set in application.conf.

View now:

class="nav-link" href="https://my.website.com" target="_blank">

This doesn't work:

class="nav-link" href=current.configuration.getString("client.server.url") target="_blank">

application.conf:

client.server.url = "https://my.website.com"

Any help would be appreciated.

3
is this Play Framework - Twirl Templates? - pme

3 Answers

0
votes

In order to use Configuration inside a Play Template you need to inyect it into the Controller and then give it to the view though its constructor.

@Singleton
class FooController @Inject()(config:Configuration, cc: ControllerComponents) extends AbstractController(cc) {

  def bar = Action {
    Ok(views.html.baz(config))
  }

}

then your view baz.scala.html

@(config:play.api.Configuration)

<a class="nav-link" href="@config.getString("client.server.url")" target="_blank">LINK</a>
0
votes

I use e.g. @{play.Play.application.configuration.getString("play.http.context")} but it might be deprecated in version 2.6. Just exchange play.http.context with your config parameter.

0
votes

There are multiple ways to access configurations in Play using Scala

The following works on Play 2.7.x

Option 1: With DI

import play.api.Configuration
.... other imports ...

class MyActor @Inject()(config: Configuration) extends Actor  {
 println(config.get[String]("akka_actor_custom_dispatcher"))
 println(config.get[String]("akka_actor_custom_dispatcher")) // w/o optional
 println(config.getOptional[Int]("value_1").getOrElse(2))    // with optional 
 .....
 }

Option 2: w/o DI

import play.api.Configuration
.... other imports ...

class MyActor() extends Actor {
 val config = new Configuration(ConfigFactory.load("application.conf"))
 println(config.get[String]("akka_actor_custom_dispatcher"))
 println(config.get[String]("akka_actor_custom_dispatcher")) // w/o optional
 println(config.getOptional[Int]("value_1").getOrElse(2))    // with optional 
 .....
 }