Access boolean values โ€‹โ€‹from application.conf in scala template

I have a boolean parameter in application.conf:

system.debugMode = false 

And I'm trying to deploy based on the value of this in my scala template:

 <p>Debug mode parameter value: @Play.current.configuration.getBoolean("system.debugMode")</p> @if(Play.current.configuration.getBoolean("system.debugMode")) { <p>Debug mode on</p> } else { <p>Debug mode off</p> } 

I would expect the debug output mode to be disabled, but what I actually see is as follows:

 Debug mode parameter value: false Debug mode on 

Do I have a problem with casting? It seems my value is being returned from the configuration file as "false", but the @if operator evaluates it as true. I note that the API states that the getBoolean method returns a parameter containing a boolean, so maybe this cannot be put in an if?

+6
source share
1 answer

play.api.Configuration.getBoolean() returns Option[Boolean] . In the game template engine, an Option containing Some(...) will always be evaluated as true in the if condition, even if the option contains Some(false) .

As a test, I created all the possible values โ€‹โ€‹for Option[Boolean] and checked what happened to them inside @if(...) in the template.

Controller:

 object Application extends Controller { def index = Action { val a: Option[Boolean] = None val b: Option[Boolean] = Some(true) val c: Option[Boolean] = Some(false) Ok(views.html.index(a, b, c)) } } 

Template:

 @(a: Option[Boolean], b: Option[Boolean], c: Option[Boolean]) @if(a) { a } @if(b) { b } @if(c) { c } 

Performing this task gives the result "bc".

If your configuration parameter has a default value, get getOrElse parameter getOrElse :

 Play.current.configuration.getBoolean("system.debugMode").getOrElse(defaultValue) 

If you are sure that the configuration parameter will always be there (or you are satisfied that your template reports that the debugging mode is disabled if the parameter is not set), you can also flatten :

 Play.current.configuration.getBoolean("system.debugMode").flatten 
+6
source

Source: https://habr.com/ru/post/953978/


All Articles