playframework/Play20 — pull request #942 by julienrf (2013-04-03 15:10:30+00:00)
Title: Clean template formats and add support for JavaScript templates

See alse #761 for the motivation behind the support of JavaScript templates.

This pull request mixes addition of new features (support of JavaScript templates), existing code clean up and some API simplifications (that may break source compatibility). Hopefully I broke it in separate commits so you can just pick the ones you are interested in. For instance, 8e3c310 can be merged to the 2.1.x branch since it adds no feature and breaks nothing, it just cleans things up.

Here is a human readable summary of this pull request:

8e3c310 (no API change, source compatible):

* Complexity of the code for defining built-in template formats is reduced (things are just factored out to avoid code duplication) ;
* Introduction of a `BufferedContent` class helping programmers to define custom template formats. As an example, consider the current way to define a custom template format:

```scala
class JavaScript extends play.templates.Appendable[JavaScript] with Content {

private val buffer = new StringBuilder

def this(content: String) = {
this()
buffer.append(content)
}

def body: String = buffer.toString()

override def toString = body

def contentType: String = MimeTypes.JAVASCRIPT

def +=(other: JavaScript): JavaScript = {
buffer.append(other.buffer)
this
}

}

object JavaScript {
def apply(text: String) = new JavaScript(text)
}
```

And the new one:
```scala
class JavaScript(buffer: StringBuilder) extends BufferedContent[JavaScript](buffer) {
val contentType = MimeTypes.JAVASCRIPT
}

object JavaScript {
def apply(content: String) = new JavaScript(new StringBuilder(content))
}
```

2a61fad (breaks source compatibility):

* Document the way to support custom template formats ;
* Simplify the sbt related things to write to support custom template formats. As an example, consider the current way to support a custom template format:

```scala
templatesTypes <<= (templatesTypes) (_ orElse {
case "js" => ("templates.JavaScript", "templates.JavaScriptFormat")
})
```
And the new one:

```scala
templatesTypes += "js" -> "templates.JavaScriptFormat"
```

1c14ed1:

* Add built-in support for JavaScript templates ;
* Fixes the scala/websocketchat sample to avoid XSS attacks. As an example, try the current version of the sample with usernames such as ``, `

Hello

` or `"`. All these cases work fine with this pull request.