Replies: 1 comment
-
I don't think this is easily possible with html format. If you look at the twirl scala code that gets generated in your target folder and debug what is getting called, you will end up here: case v if v != null => format.escape(v.toString) so if twirl does not know about the special class you are using, it will always call it's trait HtmlElement {
def render: Html
override def toString: String = render.toString // will give you raw html
} the Twirl's HTML format only outputs raw html when you pass a Maybe there is a way to automatically convert your TwirlKeys.templateImports += "views.html.TemplateImplicits._" and here I have an example package views.html
import play.twirl.api.Html
import play.twirl.api.HtmlFormat
object TemplateImplicits {
// does not work
implicit def htmlElement2html(he: HtmlElement): Html = he.render
// this does work so you can call @Btn("foo").raw but does not give you a benefit over calling render...
implicit class RichHtmlElement(he: HtmlElement) {
def raw(): Html = he.render
}
} |
Beta Was this translation helpful? Give feedback.
-
Is it possible to customize the rendering of a Scala class in Twirl, other than Twirl simply calling the class'
toString
method?https://www.playframework.com/documentation/3.0.x/ScalaTemplates#Displaying-Scala-types
Example
When a class of type
HtmlElement
is used in Twirl, I'd like it to be calling therender
method and include the return value unescaped, instead of doing atoString
.I'd like those two calls to both result in the HTML output.
Beta Was this translation helpful? Give feedback.
All reactions