How to pass an optional parameter to a scala template in playback platform 2

Is it possible for the java controller in playframework 2 to pass an optional parameter to the scala page?

I have a scala page that I process from various actions. only in a specific case, one of these actions should pass the scala parameter, do I need to change every render call? basically i want to call template.scala.html in two ways

template.render(msg) //java 

and

 template.render()//java 

where in my template I have: @ (msg: String = "xyz")

I am currently receiving an error message, without a message, render (java.lang.String) in views.html.template cannot be applied to ()

+4
source share
2 answers

In Play Framework 2 for Java, the controller must pass a default value or null. In other templates, you can leave an optional parameter.

+4
source

As a working order, you can save your parameter on the Context.args map. This is a Map<String,Object> so that you can store everything you need for the current request.

This is an easy way to make values ​​available in your templates without having to declare / pass them as parameters.

controller

 public static Result someAction() { ctx().args.put("msg", "Hello world!"); return ok(myview.render()); } 

template

 @if(ctx.args.containsKey("msg")){ <p>@ctx.args.get("msg")</p> } 
+8
source

All Articles