diff --git a/docs/manual/working/scalaGuide/main/templates/ScalaTemplates.md b/docs/manual/working/scalaGuide/main/templates/ScalaTemplates.md index 536153b8..eb332edf 100644 --- a/docs/manual/working/scalaGuide/main/templates/ScalaTemplates.md +++ b/docs/manual/working/scalaGuide/main/templates/ScalaTemplates.md @@ -168,4 +168,16 @@ For example to output raw HTML: The template engine can be used as a [string interpolator](http://docs.scala-lang.org/overviews/core/string-interpolation.html). You basically trade the “@” for a “$”: -@[string-interpolation](code/ScalaTemplates.scala) \ No newline at end of file +@[string-interpolation](code/ScalaTemplates.scala) + +## Displaying Scala types + +Twirl typically renders values of Scala types by calling `toString` method on them. However, if values are wrapped inside `Option` or collections (`Seq`, `Array`, `TraversableOnce`), Twirl first unwraps the values and then calls `toString`. + +For example, + +@[display-scala-types](code/scalaguide/templates/displayScalaTypes.scala.html) + +is rendered in the browser as + +[[images/displayScalaTypes.png]] \ No newline at end of file diff --git a/docs/manual/working/scalaGuide/main/templates/code/ScalaTemplates.scala b/docs/manual/working/scalaGuide/main/templates/code/ScalaTemplates.scala index 04863b64..db1c7280 100644 --- a/docs/manual/working/scalaGuide/main/templates/code/ScalaTemplates.scala +++ b/docs/manual/working/scalaGuide/main/templates/code/ScalaTemplates.scala @@ -147,5 +147,16 @@ object ScalaTemplatesSpec extends Specification { body must contain("") } } + + "display Scala types" in { + val body = html.displayScalaTypes().body + + body must contain("User(Foo,Bar)") + body must contain("value inside option") + body must not contain("Option(value inside option)") + body must contain("firstlast") + body must not contain("List") + body must contain("helloUser(Foo,Bar)value inside optionfirstlast") + } } } \ No newline at end of file diff --git a/docs/manual/working/scalaGuide/main/templates/code/scalaguide/templates/displayScalaTypes.scala.html b/docs/manual/working/scalaGuide/main/templates/code/scalaguide/templates/displayScalaTypes.scala.html new file mode 100644 index 00000000..6a37f561 --- /dev/null +++ b/docs/manual/working/scalaGuide/main/templates/code/scalaguide/templates/displayScalaTypes.scala.html @@ -0,0 +1,12 @@ +@import scalaguide.templates._ + +@() + +@* #display-scala-types *@ + +@* #display-scala-types *@ \ No newline at end of file diff --git a/docs/manual/working/scalaGuide/main/templates/images/displayScalaTypes.png b/docs/manual/working/scalaGuide/main/templates/images/displayScalaTypes.png new file mode 100644 index 00000000..d519a593 Binary files /dev/null and b/docs/manual/working/scalaGuide/main/templates/images/displayScalaTypes.png differ