Joao Pereira
Joao Pereira

Reputation: 2534

Render Play 2.0 view programmatically

Using Play 2.1.0, I have a Java controller with an action responsible to render arbitrary html views.

For example:

class HtmlClientViews extends Controller {    
    public static void getView(String viewName) {
        return ok(/*How to render the view programmatically?*/)
    }
}

And in my views I have a view named account.html.scala.

I have a route like:

GET   /htmlclient/*viewName        controllers.HtmlClientViews.getView(viewName)

If I make a request like /htmlclient/account.html I want to render the view named account.html.scala

I haven't tried yet to use Java reflection mechanisms for this, but would like to know what is the most effective way to achieve this.

Upvotes: 0

Views: 169

Answers (1)

biesior
biesior

Reputation: 55798

You can do it with:

  1. reflections, like in Play Authenticate usage sample, there it's used for selecting different view depending on detected language
  2. If you have known number of views you can use simple switch statement in controller to return view a,b,c or d.
  3. Also as in case no. 2 - you can use matching statement in the view to include sub-view depending on some variable.

Upvotes: 1

Related Questions