forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Change the type of templatesTypes sbt key to Map[String, String] (t…
…he template result type can be retrieved using a type member of the format) - Add some API documentation to play.templates public API - Include play-templates Scaladoc - Document how to support a custom template format
- Loading branch information
Showing
15 changed files
with
180 additions
and
23 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
documentation/manual/javaGuide/main/templates/JavaCustomTemplateFormat.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Adding support for a custom format to the template engine | ||
|
||
The built-in template engine supports common template formats (HTML, XML, etc.) but you can easily add support for your own formats, if needed. This page summarizes the steps to follow to support a custom format. | ||
|
||
## Overview of the the templating process | ||
|
||
The template engine builds its result by appending static and dynamic content parts of a template. Consider for instance the following template: | ||
|
||
``` | ||
foo @bar baz | ||
``` | ||
|
||
It consists in two static parts (`foo ` and ` baz`) around one dynamic part (`bar`). The template engine concatenates these parts together to build its result. Actually, in order to prevent cross-site scripting attacks, the value of `bar` can be escaped before being concatenated to the rest of the result. This escaping process is specific to each format: e.g. in the case of HTML you want to transform “<” into “&lt;”. | ||
|
||
How does the template engine know which format correspond to a template file? It looks at its extension: e.g. if it ends with `.scala.html` it associates the HTML format to the file. | ||
|
||
In summary, to support your own template format you need to perform the following steps: | ||
|
||
* Implement the text integration process for the format ; | ||
* Associate a file extension to the format. | ||
|
||
## Implement a format | ||
|
||
Implement the `play.templates.Format<A>` interface that has the methods `A raw(String text)` and `A escape(String text)` that will be used to integrate static and dynamic template parts, respectively. | ||
|
||
The type parameter `A` of the format defines the result type of the template rendering, e.g. `Html` for a HTML template. This type must be a subtype of the `play.templates.Appendable<A>` trait that defines how to concatenates parts together. | ||
|
||
For convenience, Play provides a `play.api.templates.BufferedContent<A>` abstract class that implements `play.templates.Appendable<A>` using a `StringBuilder` to build its result and that implements the `play.mvc.Content` interface so Play knows how to serialize it as an HTTP response body. | ||
|
||
In short, you need to write to classes: one defining the result (implementing `play.templates.Appendable<A>`) and one defining the text integration process (implementing `play.templates.Format<A>`). For instance, here is how the HTML format could be defined: | ||
|
||
```java | ||
public class Html extends BufferedContent<Html> { | ||
public Html(StringBuilder buffer) { | ||
super(buffer); | ||
} | ||
String contentType() { | ||
return "text/html"; | ||
} | ||
} | ||
|
||
public class HtmlFormat implements Format<Html> { | ||
Html raw(String text: String) { … } | ||
Html escape(String text) { … } | ||
public static final HtmlFormat instance = new HtmlFormat(); // The build process needs a static reference to the format (see the next section) | ||
} | ||
``` | ||
|
||
## Associate a file extension to the format | ||
|
||
The templates are compiled into a `.scala` files by the build process just before compiling the whole application sources. The `sbt.PlayKeys.templatesTypes` key is a sbt setting of type `Map[String, String]` defining the mapping between file extensions and template formats. For instance, if you want Play to use your onw HTML format implementation you have to write the following in your build file to associate the `.scala.html` files to your custom `my.HtmlFormat` format: | ||
|
||
```scala | ||
templatesTypes += ("html" -> "my.HtmlFormat.instance") | ||
``` | ||
|
||
Note that the right side of the arrow contains the fully qualified name of a static value of type `play.templates.Format<?>`. | ||
|
||
> **Next:** [[HTTP form submission and validation | JavaForms]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
documentation/manual/scalaGuide/main/templates/ScalaCustomTemplateFormat.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Adding support for a custom format to the template engine | ||
|
||
The built-in template engine supports common template formats (HTML, XML, etc.) but you can easily add support for your own formats, if needed. This page summarizes the steps to follow to support a custom format. | ||
|
||
## Overview of the the templating process | ||
|
||
The template engine builds its result by appending static and dynamic content parts of a template. Consider for instance the following template: | ||
|
||
``` | ||
foo @bar baz | ||
``` | ||
|
||
It consists in two static parts (`foo ` and ` baz`) around one dynamic part (`bar`). The template engine concatenates these parts together to build its result. Actually, in order to prevent cross-site scripting attacks, the value of `bar` can be escaped before being concatenated to the rest of the result. This escaping process is specific to each format: e.g. in the case of HTML you want to transform “<” into “&lt;”. | ||
|
||
How does the template engine know which format correspond to a template file? It looks at its extension: e.g. if it ends with `.scala.html` it associates the HTML format to the file. | ||
|
||
Finally, you usually want your template files to be used as the body of your HTTP responses, so you have to define how to make a Play result from a template rendering result. | ||
|
||
In summary, to support your own template format you need to perform the following steps: | ||
|
||
* Implement the text integration process for the format ; | ||
* Associate a file extension to the format ; | ||
* Eventually tell Play how to send the result of a template rendering as an HTTP response body. | ||
|
||
## Implement a format | ||
|
||
Implement the `play.templates.Format[A]` trait that has the methods `raw(text: String): A` and `escape(text: String): A` that will be used to integrate static and dynamic template parts, respectively. | ||
|
||
The type parameter `A` of the format defines the result type of the template rendering, e.g. `Html` for a HTML template. This type must be a subtype of the `play.templates.Appendable[A]` trait that defines how to concatenates parts together. | ||
|
||
For convenience, Play provides a `play.api.templates.BufferedContent[A]` abstract class that implements `play.templates.Appendable[A]` using a `StringBuilder` to build its result and that implements the `play.api.mvc.Content` trait so Play knows how to serialize it as an HTTP response body (see the last section of this page for details). | ||
|
||
In short, you need to write to classes: one defining the result (implementing `play.templates.Appendable[A]`) and one defining the text integration process (implementing `play.templates.Format[A]`). For instance, here is how the HTML format is defined: | ||
|
||
```scala | ||
// The `Html` result type. We extend `BufferedContent[Html]` rather than just `Appendable[Html]` so | ||
// Play knows how to make an HTTP result from a `Html` value | ||
class Html(buffer: StringBuilder) extends BufferedContent[Html](buffer) { | ||
val contentType = MimeTypes.HTML | ||
} | ||
|
||
object HtmlFormat extends Format[Html] { | ||
def raw(text: String): Html = … | ||
def escape(text: String): Html = … | ||
} | ||
``` | ||
|
||
## Associate a file extension to the format | ||
|
||
The templates are compiled into a `.scala` files by the build process just before compiling the whole application sources. The `sbt.PlayKeys.templatesTypes` key is a sbt setting of type `Map[String, String]` defining the mapping between file extensions and template formats. For instance, if HTML was not supported out of the box by Play, you would have to write the following in your build file to associate the `.scala.html` files to the `play.api.templates.HtmlFormat` format: | ||
|
||
```scala | ||
templatesTypes += ("html" -> "play.api.templates.HtmlFormat") | ||
``` | ||
|
||
Note that the right side of the arrow contains the fully qualified name of a value of type `play.templates.Format[_]`. | ||
|
||
## Tell Play how to make an HTTP result from a template result type | ||
|
||
Play can write an HTTP response body for any value of type `A` for which it exists an implicit `play.api.http.Writeable[A]` value. So all you need is to define such a value for your template result type. For instance, here is how to define such a value for HTTP: | ||
|
||
```scala | ||
implicit def writableHttp(implicit codec: Codec): Writeable[Http] = | ||
Writeable[Http](result => codec.encode(result.body), Some(ContentTypes.HTTP)) | ||
``` | ||
|
||
> **Note:** if your template result type extends `play.api.templates.BufferedContent` you only need to define an | ||
> implicit `play.api.http.ContentTypeOf` value: | ||
> ```scala | ||
> implicit def contentTypeHttp(implicit codec: Codec): ContentTypeOf[Http] = | ||
> ContentTypeOf[Http](Some(ContentTypes.HTTP)) | ||
> ``` | ||
> **Next:** [[HTTP form submission and validation | ScalaForms]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters