Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to Html in Play Framework

I have a simple view (Play! template) that accepts an Html object. I know now that this object is play.twirl.api.Html (in Java). In the controller I want to render. return ok(layout.render("<h1> something </h1>")); I'm using Play! 2.3.x

But I fail to find a valid conversion from String to Html. I've tried casting and setting the String as the ctor argument for a new Html object but all failed. I have found no documentation on the twirl api.

Here is my question: How do I convert a String to Html in Play! (Java) without changing the template?

like image 516
Ultimate Hawk Avatar asked Sep 08 '14 19:09

Ultimate Hawk


1 Answers

You have two possibilities (anyway in both you need to modify your template)

First is escaping HTML src within the view:

public static Result foo() {
    return ok(foo.render("<h1>Foo</h1>"));
}

View foo.scala.html:

@(myHeader: String)

@Html(myHeader)

Second is passing ready-to-use Html param:

import play.twirl.api.Html;

//....

public static Result bar() {
    return ok(bar.render(Html.apply("<h1>Bar</h1>")));
}

View bar.scala.html

@(myHeader: Html)

@myHeader
like image 189
biesior Avatar answered Oct 17 '22 20:10

biesior