Convert String to Html in Play Framework

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

But I can not find the correct conversion from String to Html. I tried casting and set String as the ctor argument for the new Html object, but all failed. I did not find the twirl api documentation.

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

+6
source share
1 answer

You have two options (in any case, you need to change your template)

The first hides the HTML src in the view:

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

View foo.scala.html :

 @(myHeader: String) @Html(myHeader) 

The second passes the ready-to-use Html parameter:

 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 
+6
source

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


All Articles