Thursday, 15 January 2015

playframework - How do I construct an Html object from a String variable in Java controller code within the play (2.3) framework -



playframework - How do I construct an Html object from a String variable in Java controller code within the play (2.3) framework -

i just getting started working play framework, , i'm trying understand interaction between java application code, , scala-based template framework (note: know absolutely nil scala far, beyond fact it's language compiles bytecode on jvm, , scala , java classes can interact).

i have test1.scala.html template looks this:

@(title: string)(content: html) <!doctype html> <html> <head> <title>@title</title> </head> <body> @content </body> </html>

as can see top line, template expects string , html argument, can't figure out how build html argument java caller code!

i have tried few variations in controller class:

return ok(test1.render("my title","it <em>finally</em> works!"));

this fails, obviously, because sec argument string , not html, have argument mismatch. (there's runtime error: actual argument string cannot converted html method invocation conversion -- makes sense, hoping magic here. :))

so tried creating html string, figuring helper class somewhere in bundle , might 'just work':

return ok(test1.render("my title",new html("it <em>finally</em> works!")));

this won't compile, because javac can't find html class. ok, fair enough. scanning play documentation, there appears play.api.templates.html class (written in scala) constructor takes string, seek total package-qualified name:

return ok(test1.render("my title",new play.api.templates.html("it <em>finally</em> works!")));

and won't compile either: symbol not found 'html' in bundle play.api.templates.

so: what's magic sauce allow me turn string (which contains snippet of html) html object can pass template?

play templates have been factored out twirl module, stated in play 2.3 migration guide.

play.api.templates.html play.twirl.api.html.

java playframework playframework-2.3

No comments:

Post a Comment