I am just getting started working with the play framework, and I'm trying to understand the interaction between java application code, and the scala-based template framework (Note: I know absolutely nothing about Scala so far, beyond the fact that it's another language that compiles to bytecode on the JVM, and that your Scala and Java classes can interact).
I have a test1.scala.html template that looks like this:
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
</head>
<body>
@content
</body> </html>
As you can see from the top line, the template expects a String and an Html argument, but I can't figure out how to construct the Html argument from Java caller code!
I have tried a few variations in my controller class:
return ok(test1.render("My Title","It <em>finally</em> works!"));
This fails, obviously, because the second argument is a String and not Html, so I have an argument mismatch. (There's a runtime error: actual argument String cannot be converted to Html by method invocation conversion
-- which makes sense, but I was hoping for some magic here. :))
So I tried creating some Html from a String, figuring this was a likely helper class somewhere in the package and this 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 an Html class. Ok, fair enough. Scanning the play documentation, there appears to be a play.api.templates.Html class (written in Scala) with a constructor that takes a String, so I try the full package-qualified name:
return ok(test1.render("My Title",new play.api.templates.Html("It <em>finally</em> works!")));
And this won't compile either: I get a Symbol not found for 'Html' in package play.api.templates.
So: what's the magic sauce that will let me turn my String (which contains a snippet of HTML) into an HTML object I can pass into the template?
Play templates have been factored out into the Twirl module, as stated in the Play 2.3 Migration Guide.
play.api.templates.Html
is now play.twirl.api.Html
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With