Search code examples
scalatwirl

How to write rendered template output to file?


I'm using Twirl in a non-play sbt project, and defined a template:

hello.scala.html

<h1>Welcome hello world</h1>

It generates a Scala file containing the following code:

package html

import play.twirl.api._
import play.twirl.api.TemplateMagic._

import io.github.freewind.feverblog._

/**/
object hello extends BaseScalaTemplate[play.twirl.api.HtmlFormat.Appendable,Format[play.twirl.api.HtmlFormat.Appendable]](play.twirl.api.HtmlFormat) with play.twirl.api.Template0[play.twirl.api.HtmlFormat.Appendable] {

  /**/
  def apply():play.twirl.api.HtmlFormat.Appendable = {
      _display_ {
        Seq[Any](format.raw/*1.1*/("""<h1>Welcome hello world</h1>"""))
      }
  }

  def render(): play.twirl.api.HtmlFormat.Appendable = apply()

  def f:(() => play.twirl.api.HtmlFormat.Appendable) = () => apply()

  def ref: this.type = this
}

The return type of render() and apply() is play.twirl.api.HtmlFormat.Appendable. How can I convert it to a String so I can write it to a file?


Solution

  • play.twirl.api.HtmlFormat.Appendable is just a type alias for play.twirl.api.Html, which has a toString method.

    i.e.

    views.html.hello().toString