| 1 |
////////////////////////////////////////// |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
or more contributor license agreements. See the NOTICE file |
| 5 |
distributed with this work for additional information |
| 6 |
regarding copyright ownership. The ASF licenses this file |
| 7 |
to you under the Apache License, Version 2.0 (the |
| 8 |
"License"); you may not use this file except in compliance |
| 9 |
with the License. You may obtain a copy of the License at |
| 10 |
|
| 11 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
|
| 13 |
Unless required by applicable law or agreed to in writing, |
| 14 |
software distributed under the License is distributed on an |
| 15 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
KIND, either express or implied. See the License for the |
| 17 |
specific language governing permissions and limitations |
| 18 |
under the License. |
| 19 |
|
| 20 |
////////////////////////////////////////// |
| 21 |
|
| 22 |
= The MarkupTemplateEngine |
| 23 |
|
| 24 |
This template engine is a template engine primarily aimed at generating XML-like markup (XML, XHTML, HTML5, ...), but that |
| 25 |
can be used to generate any text based content. Unlike traditional template engines, this one relies on a DSL that uses the |
| 26 |
builder syntax. Here is a sample template: |
| 27 |
|
| 28 |
[source,groovy] |
| 29 |
---- |
| 30 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example1_template,indent=0] |
| 31 |
---- |
| 32 |
|
| 33 |
If you feed it with the following model: |
| 34 |
|
| 35 |
[source,groovy] |
| 36 |
---- |
| 37 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example1_model,indent=0] |
| 38 |
---- |
| 39 |
|
| 40 |
It would be rendered as: |
| 41 |
|
| 42 |
[source,xml] |
| 43 |
---- |
| 44 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example1_expected,indent=0] |
| 45 |
---- |
| 46 |
|
| 47 |
The key features of this template engine are: |
| 48 |
|
| 49 |
* a _markup builder like_ syntax |
| 50 |
* templates are compiled into bytecode |
| 51 |
* fast rendering |
| 52 |
* optional type checking of the model |
| 53 |
* includes |
| 54 |
* internationalization support |
| 55 |
* fragments/layouts |
| 56 |
|
| 57 |
== The template format |
| 58 |
=== Basics |
| 59 |
|
| 60 |
Templates consist of Groovy code. Let's explore the first example more thoroughly: |
| 61 |
|
| 62 |
[source,groovy] |
| 63 |
---- |
| 64 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example1_template_with_bullets,indent=0] |
| 65 |
---- |
| 66 |
<1> renders the XML declaration string. |
| 67 |
<2> opens a `cars` tag |
| 68 |
<3> `cars` is a variable found in the _template model_, which is a list of `Car` instances |
| 69 |
<4> for each item, we create a `car` tag with the attributes from the `Car` instance |
| 70 |
<5> closes the `cars` tag |
| 71 |
|
| 72 |
As you can see, regular Groovy code can be used in the template. Here, we are calling `each` on a list (retrieved from the model), allowing us to |
| 73 |
render one `car` tag per entry. |
| 74 |
|
| 75 |
In a similar fashion, rendering HTML code is as simple as this: |
| 76 |
|
| 77 |
[source,groovy] |
| 78 |
---- |
| 79 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example2_template,indent=0] |
| 80 |
---- |
| 81 |
<1> renders the HTML doctype special tag |
| 82 |
<2> opens the `html` tag with an attribute |
| 83 |
<3> opens the `head` tag |
| 84 |
<4> renders a `meta` tag with one `http-equiv` attribute |
| 85 |
<5> renders the `title` tag |
| 86 |
<6> closes the `head` tag |
| 87 |
<7> opens the `body` tag |
| 88 |
<8> renders a `p` tag |
| 89 |
<9> closes the `body` tag |
| 90 |
<10> closes the `html` tag |
| 91 |
|
| 92 |
The output is straightforward: |
| 93 |
|
| 94 |
[source,html] |
| 95 |
---- |
| 96 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=example2_expected,indent=0] |
| 97 |
---- |
| 98 |
|
| 99 |
NOTE: With some <<markuptemplate-config,configuration>>, you can have the output pretty printed, with newlines and indent automatically added. |
| 100 |
|
| 101 |
=== Support methods |
| 102 |
|
| 103 |
In the previous example, the doctype declaration was rendered using the `yieldUnescaped` method. We have also seen the `xmlDeclaration` method. |
| 104 |
The template engine provides several support methods that will help you render contents appropriately: |
| 105 |
|
| 106 |
[cols="1,2,3a",options="header"] |
| 107 |
|======================================================================= |
| 108 |
|Method|Description|Example |
| 109 |
|yield |
| 110 |
|Renders contents, but escapes it before rendering |
| 111 |
|*Template*: |
| 112 |
```groovy |
| 113 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=yield,indent=0] |
| 114 |
``` |
| 115 |
|
| 116 |
*Output*: |
| 117 |
```html |
| 118 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=yield_expected,indent=0] |
| 119 |
``` |
| 120 |
|yieldUnescaped |
| 121 |
|Renders raw contents. The argument is rendered as is, without escaping. |
| 122 |
|*Template*: |
| 123 |
```groovy |
| 124 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=yieldUnescaped,indent=0] |
| 125 |
``` |
| 126 |
|
| 127 |
*Output*: |
| 128 |
```html |
| 129 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=yieldUnescaped_expected,indent=0] |
| 130 |
``` |
| 131 |
|
| 132 |
|xmlDeclaration |
| 133 |
|Renders an XML declaration String. If the encoding is specified in the configuration, it is written in the declaration. |
| 134 |
|*Template*: |
| 135 |
```groovy |
| 136 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration,indent=0] |
| 137 |
``` |
| 138 |
|
| 139 |
*Output*: |
| 140 |
```html |
| 141 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration_expected,indent=0] |
| 142 |
``` |
| 143 |
|
| 144 |
If `TemplateConfiguration#getDeclarationEncoding` is not null: |
| 145 |
|
| 146 |
*Output*: |
| 147 |
```html |
| 148 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration_encoding_expected,indent=0] |
| 149 |
``` |
| 150 |
|
| 151 |
|comment |
| 152 |
|Renders raw contents inside an XML comment |
| 153 |
|*Template*: |
| 154 |
```groovy |
| 155 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=comment,indent=0] |
| 156 |
``` |
| 157 |
|
| 158 |
*Output*: |
| 159 |
```html |
| 160 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=comment_expected,indent=0] |
| 161 |
``` |
| 162 |
|
| 163 |
|newLine |
| 164 |
|Renders a new line. See also `TemplateConfiguration#setAutoNewLine` and `TemplateConfiguration#setNewLineString`. |
| 165 |
|*Template*: |
| 166 |
```groovy |
| 167 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=newline,indent=0] |
| 168 |
``` |
| 169 |
|
| 170 |
*Output*: |
| 171 |
```html |
| 172 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=newline_expected,indent=0] |
| 173 |
``` |
| 174 |
|
| 175 |
|pi |
| 176 |
|Renders an XML processing instruction. |
| 177 |
|*Template*: |
| 178 |
```groovy |
| 179 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=pi,indent=0] |
| 180 |
``` |
| 181 |
|
| 182 |
*Output*: |
| 183 |
```xml |
| 184 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=pi_expected,indent=0] |
| 185 |
``` |
| 186 |
|
| 187 |
|tryEscape |
| 188 |
|Returns an escaped string for an object, if it is a `String` (or any type derived from `CharSequence`). Otherwise returns the object itself. |
| 189 |
|*Template*: |
| 190 |
```groovy |
| 191 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=tryEscape,indent=0] |
| 192 |
``` |
| 193 |
|
| 194 |
*Output*: |
| 195 |
```html |
| 196 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=tryEscape_expected,indent=0] |
| 197 |
``` |
| 198 |
|
| 199 |
|======================================================================= |
| 200 |
|
| 201 |
=== Includes |
| 202 |
|
| 203 |
The `MarkupTemplateEngine` supports inclusion of contents from another file. Included contents may be: |
| 204 |
|
| 205 |
* another template |
| 206 |
* raw contents |
| 207 |
* contents to be escaped |
| 208 |
|
| 209 |
Including another template can be done using: |
| 210 |
|
| 211 |
[source,groovy] |
| 212 |
---- |
| 213 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=include_template,indent=0] |
| 214 |
---- |
| 215 |
|
| 216 |
Including a file as raw contents, without escaping it, can be done like this: |
| 217 |
|
| 218 |
[source,groovy] |
| 219 |
---- |
| 220 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=include_raw,indent=0] |
| 221 |
---- |
| 222 |
|
| 223 |
Eventually, inclusion of text that should be escaped before rendering can be done using: |
| 224 |
|
| 225 |
[source,groovy] |
| 226 |
---- |
| 227 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=include_escaped,indent=0] |
| 228 |
---- |
| 229 |
|
| 230 |
Alternatively, you can use the following helper methods instead: |
| 231 |
|
| 232 |
* `includeGroovy(<name>)` to include another template |
| 233 |
* `includeEscaped(<name>)` to include another file with escaping |
| 234 |
* `includeUnescaped(<name>)` to include another file without escaping |
| 235 |
|
| 236 |
Calling those methods instead of the `include xxx:` syntax can be useful if the name of the file to be included is dynamic (stored in a variable for example). |
| 237 |
Files to be included (independently of their type, template or text) are found on *classpath*. This is one of the reasons why the `MarkupTemplateEngine` takes |
| 238 |
an optional `ClassLoader` as constructor argument (the other reason being that you can include code referencing other classes in a template). |
| 239 |
|
| 240 |
If you don't want your templates to be on classpath, the `MarkupTemplateEngine` accepts a convenient constructor that lets you define the directory where |
| 241 |
templates are to be found. |
| 242 |
|
| 243 |
=== Fragments |
| 244 |
|
| 245 |
Fragments are nested templates. They can be used to provide improved composition in a single template. A fragment consists of |
| 246 |
a string, the inner template, and a model, used to render this template. Consider the following template: |
| 247 |
|
| 248 |
[source,groovy] |
| 249 |
---- |
| 250 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=fragment_template,indent=0] |
| 251 |
---- |
| 252 |
|
| 253 |
The `fragment` element creates a nested template, and renders it with a model which is specific to this template. Here, |
| 254 |
we have the `li(line)` fragment, where `line` is bound to `it`. Since `it` corresponds to the iteration of `pages`, |
| 255 |
we will generate a single `li` element for each page in our model: |
| 256 |
|
| 257 |
[source,html] |
| 258 |
---- |
| 259 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=fragment_expected,indent=0] |
| 260 |
---- |
| 261 |
|
| 262 |
Fragments are interesting to factorize template elements. They come at the price of the compilation of a fragment per template, and they cannot |
| 263 |
be externalized. |
| 264 |
|
| 265 |
=== Layouts |
| 266 |
|
| 267 |
Layouts, unlike fragments, refer to other templates. They can be used to compose templates and share common structures. This is often |
| 268 |
interesting if you have, for example, a common HTML page setup, and that you only want to replace the body. This can be done easily |
| 269 |
with a _layout_. First of all, you need to create a layout template: |
| 270 |
|
| 271 |
[source,groovy] |
| 272 |
.layout-main.tpl |
| 273 |
---- |
| 274 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test-resources/layout-main.tpl[tags=layout_main,indent=0] |
| 275 |
---- |
| 276 |
<1> the `title` variable (inside the title tag) is a layout variable |
| 277 |
<2> the `bodyContents` call will render the body |
| 278 |
|
| 279 |
Then what you need is a template that includes the layout: |
| 280 |
|
| 281 |
[[example-layout-simple]] |
| 282 |
[source,groovy] |
| 283 |
---- |
| 284 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_template,indent=0] |
| 285 |
---- |
| 286 |
<1> use the `main-layout.tpl` layout file |
| 287 |
<2> set the `title` variable |
| 288 |
<3> set the `bodyContents` |
| 289 |
|
| 290 |
As you can see, `bodyContents` will be rendered inside the layout, thanks to the `bodyContents()` call in the layout file. As |
| 291 |
a result, the template will be rendered as this: |
| 292 |
|
| 293 |
[source,html] |
| 294 |
---- |
| 295 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_expected,indent=0] |
| 296 |
---- |
| 297 |
|
| 298 |
The call to the `contents` method is used to tell the template engine that the block of code is in fact a specification of a |
| 299 |
template, instead of a helper function to be rendered directly. If you don't add `contents` before your specification, then |
| 300 |
the contents would be rendered, but you would also see a random string generated, corresponding to the result value of the block. |
| 301 |
|
| 302 |
Layouts are a powerful way to share common elements across multiple |
| 303 |
templates, without having to rewrite everything or use includes. |
| 304 |
|
| 305 |
Layouts use, by default, a model which is independent from the model of the page where they are used. It is however possible |
| 306 |
to make them inherit from the parent model. Imagine that the model is defined like this: |
| 307 |
|
| 308 |
[source,java] |
| 309 |
---- |
| 310 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_template_inherit_model,indent=0] |
| 311 |
---- |
| 312 |
|
| 313 |
and the following template: |
| 314 |
|
| 315 |
[source,groovy] |
| 316 |
---- |
| 317 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_template_inherit,indent=0] |
| 318 |
---- |
| 319 |
<1> note the use of `true` to enable model inheritance |
| 320 |
|
| 321 |
then it is not necessary to pass the `title` value to the layout as in the <<example-layout-simple,previous example>>. The result will be: |
| 322 |
|
| 323 |
[source,html] |
| 324 |
---- |
| 325 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_expected_2,indent=0] |
| 326 |
---- |
| 327 |
|
| 328 |
But it is also possible to override a value from the parent model: |
| 329 |
|
| 330 |
[source,groovy] |
| 331 |
---- |
| 332 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_template_inherit_override,indent=0] |
| 333 |
---- |
| 334 |
<1> `true` means inherit from the parent model |
| 335 |
<2> but `title` is overridden |
| 336 |
|
| 337 |
then the output will be: |
| 338 |
|
| 339 |
[source,html] |
| 340 |
---- |
| 341 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=layout_expected_3,indent=0] |
| 342 |
---- |
| 343 |
|
| 344 |
== Rendering contents |
| 345 |
=== Creation of a template engine |
| 346 |
On the server side, rendering templates require an instance of `groovy.text.markup.MarkupTemplateEngine` and a |
| 347 |
`groovy.text.markup.TemplateConfiguration`: |
| 348 |
|
| 349 |
[source,java] |
| 350 |
---- |
| 351 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=rendering_setup,indent=0] |
| 352 |
---- |
| 353 |
<1> creates a template configuration |
| 354 |
<2> creates a template engine with this configuration |
| 355 |
<3> creates a template instance from a `String` |
| 356 |
<4> creates a model to be used in the template |
| 357 |
<5> bind the model to the template instance |
| 358 |
<6> render output |
| 359 |
|
| 360 |
There are several possible options to parse templates: |
| 361 |
|
| 362 |
* from a `String`, using `createTemplate(String)` |
| 363 |
* from a `Reader`, using `createTemplate(Reader)` |
| 364 |
* from a `URL`, using `createTemplate(URL)` |
| 365 |
* given a template name, using `createTemplateByPath(String)` |
| 366 |
|
| 367 |
The last version should in general be preferred: |
| 368 |
|
| 369 |
[source,groovy] |
| 370 |
---- |
| 371 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=rendering_by_name,indent=0] |
| 372 |
---- |
| 373 |
|
| 374 |
[[markuptemplate-config]] |
| 375 |
=== Configuration options |
| 376 |
|
| 377 |
The behavior of the engine can be tweaked with several configuration options accessible through the `TemplateConfiguration` class: |
| 378 |
|
| 379 |
[cols="1,1,2,3a",options="header"] |
| 380 |
|======================================================================= |
| 381 |
|Option|Default value|Description|Example |
| 382 |
|declarationEncoding |
| 383 |
|null |
| 384 |
|Determines the value of the encoding to be written when `xmlDeclaration` is called. It does *not* influence the writer you are using as output. |
| 385 |
|*Template*: |
| 386 |
```groovy |
| 387 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration,indent=0] |
| 388 |
``` |
| 389 |
|
| 390 |
*Output*: |
| 391 |
```html |
| 392 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration_expected,indent=0] |
| 393 |
``` |
| 394 |
|
| 395 |
If `TemplateConfiguration#getDeclarationEncoding` is not null: |
| 396 |
|
| 397 |
*Output*: |
| 398 |
```html |
| 399 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=xmlDeclaration_encoding_expected,indent=0] |
| 400 |
``` |
| 401 |
|
| 402 |
|expandEmptyElements |
| 403 |
|false |
| 404 |
|If true, empty tags are rendered in their expanded form. |
| 405 |
|*Template*: |
| 406 |
```groovy |
| 407 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=expandEmptyElements,indent=0] |
| 408 |
``` |
| 409 |
|
| 410 |
*Output*: |
| 411 |
```html |
| 412 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=expandEmptyElements_false,indent=0] |
| 413 |
``` |
| 414 |
|
| 415 |
If `expandEmptyElements` is true: |
| 416 |
|
| 417 |
*Output*: |
| 418 |
```html |
| 419 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=expandEmptyElements_true,indent=0] |
| 420 |
``` |
| 421 |
|
| 422 |
|useDoubleQuotes |
| 423 |
|false |
| 424 |
|If true, use double quotes for attributes instead of simple quotes |
| 425 |
|*Template*: |
| 426 |
```groovy |
| 427 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=useDoubleQuotes,indent=0] |
| 428 |
``` |
| 429 |
|
| 430 |
*Output*: |
| 431 |
```html |
| 432 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=useDoubleQuotes_false,indent=0] |
| 433 |
``` |
| 434 |
|
| 435 |
If `useDoubleQuotes` is true: |
| 436 |
|
| 437 |
*Output*: |
| 438 |
```xml |
| 439 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=useDoubleQuotes_true,indent=0] |
| 440 |
``` |
| 441 |
|
| 442 |
|newLineString |
| 443 |
|System default (system property `line.separator`) |
| 444 |
|Allows to choose what string is used when a new line is rendered |
| 445 |
|*Template*: |
| 446 |
```groovy |
| 447 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=newLineString,indent=0] |
| 448 |
``` |
| 449 |
|
| 450 |
If `newLineString='BAR'`: |
| 451 |
|
| 452 |
*Output*: |
| 453 |
```html |
| 454 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=newLineString_expected,indent=0] |
| 455 |
``` |
| 456 |
|
| 457 |
|autoEscape |
| 458 |
|false |
| 459 |
|If true, variables from models are automatically escaped before rendering. |
| 460 |
|See <<markuptemplate-autoescape, the auto escape section>> |
| 461 |
|
| 462 |
|autoIndent |
| 463 |
|false |
| 464 |
|If true, performs automatic indentation after new lines |
| 465 |
|See <<markuptemplate-autoformat, the auto formatting section>> |
| 466 |
|
| 467 |
|autoIndentString |
| 468 |
|four (4) spaces |
| 469 |
|The string to be used as indent. |
| 470 |
|See <<markuptemplate-autoformat, the auto formatting section>> |
| 471 |
|
| 472 |
|autoNewLine |
| 473 |
|false |
| 474 |
|If true, performs automatic insertion of new lines |
| 475 |
|See <<markuptemplate-autoformat, the auto formatting section>> |
| 476 |
|
| 477 |
|baseTemplateClass |
| 478 |
|`groovy.text.markup.BaseTemplate` |
| 479 |
|Sets the super class of compiled templates. This can be used to provide application specific templates. |
| 480 |
|See <<markuptemplate-basetemplate, the custom templates section>> |
| 481 |
|
| 482 |
|locale |
| 483 |
|Default locale |
| 484 |
|Sets the default locale for templates. |
| 485 |
|See <<markuptemplate-i18n, the internationalization section>> |
| 486 |
|
| 487 |
|======================================================================= |
| 488 |
|
| 489 |
WARNING: Once the template engine has been created, it is *unsafe* to change the configuration. |
| 490 |
|
| 491 |
[[markuptemplate-autoformat]] |
| 492 |
=== Automatic formatting |
| 493 |
|
| 494 |
By default, the template engine will render output without any specific formatting. Some <<markuptemplate-config,configuration options>> can improve the situation: |
| 495 |
|
| 496 |
* `autoIndent` is responsible for auto-indenting after a new line is inserted |
| 497 |
* `autoNewLine` is responsible for automatically inserting new lines based on the original formatting of the template source |
| 498 |
|
| 499 |
In general, it is recommended to set both `autoIndent` and `autoNewLine` to true if you want human-readable, pretty printed, output: |
| 500 |
|
| 501 |
[source,java] |
| 502 |
---- |
| 503 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_setup,indent=0] |
| 504 |
---- |
| 505 |
|
| 506 |
Using the following template: |
| 507 |
|
| 508 |
[source,groovy] |
| 509 |
---- |
| 510 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template,indent=0] |
| 511 |
---- |
| 512 |
|
| 513 |
The output will now be: |
| 514 |
|
| 515 |
[source,html] |
| 516 |
---- |
| 517 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template_expected,indent=0] |
| 518 |
---- |
| 519 |
|
| 520 |
We can slightly change the template so that the `title` instruction is found on the same line as the `head` one: |
| 521 |
|
| 522 |
[source,groovy] |
| 523 |
---- |
| 524 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template2,indent=0] |
| 525 |
---- |
| 526 |
|
| 527 |
And the output will reflect that: |
| 528 |
|
| 529 |
[source,html] |
| 530 |
---- |
| 531 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template2_expected,indent=0] |
| 532 |
---- |
| 533 |
|
| 534 |
New lines are *only* inserted where curly braces for tags are found, and the insertion corresponds to where the nested content is found. This means that |
| 535 |
tags in the body of another tag will *not* trigger new lines unless they use curly braces themselves: |
| 536 |
|
| 537 |
[source,groovy] |
| 538 |
---- |
| 539 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template3,indent=0] |
| 540 |
---- |
| 541 |
<1> a new line is inserted because `meta` is not on the same line as `head` |
| 542 |
<2> no new line is inserted, because we're on the same depth as the previous tag |
| 543 |
<3> we can force rendering of a new line by explicitly calling `newLine` |
| 544 |
<4> and this tag will be rendered on a separate line |
| 545 |
|
| 546 |
This time, the output will be: |
| 547 |
|
| 548 |
[source,html] |
| 549 |
---- |
| 550 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoformat_template3_expected,indent=0] |
| 551 |
---- |
| 552 |
|
| 553 |
By default, the renderer uses four(4) spaces as indent, but you can change it by setting the `TemplateConfiguration#autoIndentString` property. |
| 554 |
|
| 555 |
[[markuptemplate-autoescape]] |
| 556 |
=== Automatic escaping |
| 557 |
|
| 558 |
By default, contents which is read from the model is rendered *as is*. If this contents comes from user input, it can be sensible, and you might |
| 559 |
want to escape it by default, for example to avoid XSS injection. For that, the template configuration provides an option which will automatically |
| 560 |
escape objects from the model, as long as they inherit from `CharSequence` (typically, `String`s). |
| 561 |
|
| 562 |
Let's imagine the following setup: |
| 563 |
|
| 564 |
[source,groovy] |
| 565 |
---- |
| 566 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_setup,indent=0] |
| 567 |
---- |
| 568 |
|
| 569 |
and the following template: |
| 570 |
|
| 571 |
[source,groovy] |
| 572 |
---- |
| 573 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_template,indent=0] |
| 574 |
---- |
| 575 |
|
| 576 |
Then you wouldn't want the HTML from `unsafeContents` to be rendered as is, because of potential security issues: |
| 577 |
|
| 578 |
[source,html] |
| 579 |
---- |
| 580 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_template_expected,indent=0] |
| 581 |
---- |
| 582 |
|
| 583 |
Automatic escaping will fix this: |
| 584 |
|
| 585 |
[source,groovy] |
| 586 |
---- |
| 587 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_setup_fixed,indent=0] |
| 588 |
---- |
| 589 |
|
| 590 |
And now the output is properly escaped: |
| 591 |
|
| 592 |
[source,html] |
| 593 |
---- |
| 594 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_template_fixed_expected,indent=0] |
| 595 |
---- |
| 596 |
|
| 597 |
Note that using automatic escaping doesn't prevent you from including unescaped contents from the model. To do this, your template should then explicitly |
| 598 |
mention that a model variable should not be escaped by prefixing it with `unescaped.`, like in this example: |
| 599 |
|
| 600 |
[source,html] |
| 601 |
.Explicit bypass of automatic escaping |
| 602 |
---- |
| 603 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=autoescape_template_unescaped,indent=0] |
| 604 |
---- |
| 605 |
|
| 606 |
[[markuptemplate-gotchas]] |
| 607 |
=== Common gotchas |
| 608 |
==== Strings containing markup |
| 609 |
|
| 610 |
Say that you want to generate a `<p>` tag which contains a string containing markup: |
| 611 |
|
| 612 |
[source,groovy] |
| 613 |
---- |
| 614 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_longversion,indent=0] |
| 615 |
---- |
| 616 |
|
| 617 |
and generates: |
| 618 |
|
| 619 |
[source,html] |
| 620 |
---- |
| 621 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_longversion_expected,indent=0] |
| 622 |
---- |
| 623 |
|
| 624 |
Can't this be written shorter? A naive alternative would be: |
| 625 |
|
| 626 |
[source,groovy] |
| 627 |
---- |
| 628 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_naive_fail,indent=0] |
| 629 |
---- |
| 630 |
|
| 631 |
but the result will not look as expected: |
| 632 |
|
| 633 |
[source,groovy] |
| 634 |
---- |
| 635 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_naive_fail_expected,indent=0] |
| 636 |
---- |
| 637 |
|
| 638 |
The reason is that the markup template engine is a _streaming_ engine. In the original version, the first `yield` call |
| 639 |
generates a string which is streamed to the output, then the `a` link is generated and streamed, and then the last `yield` |
| 640 |
call is streamed, leading in an execution *in order*. But with the string version above, the order of execution is different: |
| 641 |
|
| 642 |
* the `yield` call requires an argument, a _string_ |
| 643 |
* that arguments needs to be evaluated _before_ the _yield_ call is generated |
| 644 |
|
| 645 |
so evaluating the string leads to an execution of the `a(href:...)` call *before* `yield` is itself called. This is not |
| 646 |
what you want to do. Instead, you want to generate a _string_ which contains markup, which is then passed to the `yield` |
| 647 |
call. This can be done this way: |
| 648 |
|
| 649 |
[source,groovy] |
| 650 |
---- |
| 651 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_stringof,indent=0] |
| 652 |
---- |
| 653 |
|
| 654 |
Note the `stringOf` call, which basically tells the markup template engine that the underlying markup needs to be rendered |
| 655 |
separately and exported as a string. Note that for simple expressions, `stringOf` can be replaced by an alternate tag |
| 656 |
notation that starts with a _dollar_ sign: |
| 657 |
|
| 658 |
[source,groovy] |
| 659 |
---- |
| 660 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=gotcha_strings_stringof_dollar,indent=0] |
| 661 |
---- |
| 662 |
|
| 663 |
TIP: It is worth noting that using `stringOf` or the special `$tag` notation triggers the creation of a distinct string writer |
| 664 |
which is then used to render the markup. It is slower than using the version with calls to `yield` which perform direct |
| 665 |
streaming of the markup instead. |
| 666 |
|
| 667 |
[[markuptemplate-i18n]] |
| 668 |
=== Internationalization |
| 669 |
|
| 670 |
The template engine has native support for internationalization. For that, when you create the `TemplateConfiguration`, you can provide |
| 671 |
a `Locale` which is the default locale to be used for templates. Each template may have different versions, one for each locale. The |
| 672 |
name of the template makes the difference: |
| 673 |
|
| 674 |
* `file.tpl`: default template file |
| 675 |
* `file_fr_FR.tpl`: french version of the template |
| 676 |
* `file_en_US.tpl`: american english version of the template |
| 677 |
* ... |
| 678 |
|
| 679 |
When a template is rendered or included, then: |
| 680 |
|
| 681 |
* if the template name or include name *explicitly* sets a locale, the *specific* version is included, or the default version if not found |
| 682 |
* if the template name doesn't include a locale, the version for the `TemplateConfiguration` locale is used, or the default version if not found |
| 683 |
|
| 684 |
For example, imagine the default locale is set to `Locale.ENGLISH` and that the main template includes: |
| 685 |
|
| 686 |
[source,groovy] |
| 687 |
.Use an explicit locale in include |
| 688 |
---- |
| 689 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=locale_explicit_import,indent=0] |
| 690 |
---- |
| 691 |
|
| 692 |
then the template is rendered using the specific template: |
| 693 |
|
| 694 |
[source,html] |
| 695 |
.Bypass the template configuration |
| 696 |
---- |
| 697 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=locale_explicit_import_expected,indent=0] |
| 698 |
---- |
| 699 |
|
| 700 |
Using an include without specifying a locale will make the template engine look for a template with the configured locale, and if not, fallback to the default, like here: |
| 701 |
|
| 702 |
[source,groovy] |
| 703 |
.Don't use a locale in include |
| 704 |
---- |
| 705 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=locale_implicit_import,indent=0] |
| 706 |
---- |
| 707 |
|
| 708 |
[source,html] |
| 709 |
.Fallback to the default template |
| 710 |
---- |
| 711 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=locale_implicit_import_expected,indent=0] |
| 712 |
---- |
| 713 |
|
| 714 |
However, changing the default locale of the template engine to `Locale.FRANCE` will change the output, because the template engine will now look for a file |
| 715 |
with the `fr_FR` locale: |
| 716 |
|
| 717 |
[source,html] |
| 718 |
.Don't fallback to the default template because a locale specific template was found |
| 719 |
---- |
| 720 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=locale_implicit_import_expected2,indent=0] |
| 721 |
---- |
| 722 |
|
| 723 |
This strategy lets you translate your templates one by one, by relying on default templates, for which no locale is set in the file name. |
| 724 |
|
| 725 |
[[markuptemplate-basetemplate]] |
| 726 |
=== Custom template classes |
| 727 |
|
| 728 |
By default, templates created inherit the `groovy.text.markup.BaseTemplate` class. It may be interesting for an application to provide a different |
| 729 |
template class, for example to provide additional helper methods which are aware of the application, or customized rendering primitives (for HTML, |
| 730 |
for example). |
| 731 |
|
| 732 |
The template engine provides this ability by setting an alternative `baseTemplateClass` in the `TemplateConfiguration`: |
| 733 |
|
| 734 |
[source,java] |
| 735 |
---- |
| 736 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=custombase_config,indent=0] |
| 737 |
---- |
| 738 |
|
| 739 |
The custom base class has to extend `BaseClass` like in this example: |
| 740 |
|
| 741 |
[source,groovy] |
| 742 |
---- |
| 743 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MyTemplate.groovy[tags=basetemplate_class,indent=0] |
| 744 |
---- |
| 745 |
|
| 746 |
This example shows a class which provides an additional method named `hasModule`, which can then be used directly in the template: |
| 747 |
|
| 748 |
[source,groovy] |
| 749 |
---- |
| 750 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=custombase_template,indent=0] |
| 751 |
---- |
| 752 |
|
| 753 |
== Type checked templates |
| 754 |
=== Optional type checking |
| 755 |
|
| 756 |
Even if templates are not type checked, they are statically compiled. This means that once the templates are compiled, performance should be very good. For some |
| 757 |
applications, it might be good to make sure that templates are valid before they are actually rendered. This means failing template compilation, for example, if |
| 758 |
a method on a model variable doesn't exist. |
| 759 |
|
| 760 |
The `MarkupTemplateEngine` provides such a facility. Templates can be optionally type checked. For that, the developer must provide additional information at |
| 761 |
template creation time, which is the types of the variables found in the model. Imagine a model exposing a list of pages, where a page is defined as: |
| 762 |
|
| 763 |
[source,groovy] |
| 764 |
.Page.groovy |
| 765 |
---- |
| 766 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=page_class,indent=0] |
| 767 |
---- |
| 768 |
|
| 769 |
Then a list of pages can be exposed in the model, like this: |
| 770 |
|
| 771 |
[source,groovy] |
| 772 |
---- |
| 773 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=typechecked_setup_no_stc,indent=0] |
| 774 |
---- |
| 775 |
|
| 776 |
A template can use it easily: |
| 777 |
|
| 778 |
[source,groovy] |
| 779 |
---- |
| 780 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=typechecked_template,indent=0] |
| 781 |
---- |
| 782 |
<1> iterate on pages from the model |
| 783 |
<2> `page.title` is valid |
| 784 |
<3> `page.text` is *not* (should be `page.body`) |
| 785 |
|
| 786 |
Without type checking, the compilation of the template succeeds, because the template engine doesn't know about the model until a page |
| 787 |
is actually rendered. This means that the problem would only surface at runtime, once the page is rendered: |
| 788 |
|
| 789 |
.Runtime error |
| 790 |
---- |
| 791 |
No such property: text |
| 792 |
---- |
| 793 |
|
| 794 |
In some situations, this can be complicated to sort out or even notice. By declaring the type of the `pages` to the template engine, we're now capable of failing at compile time: |
| 795 |
|
| 796 |
[source,groovy] |
| 797 |
---- |
| 798 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=typechecked_setup_fixed,indent=0] |
| 799 |
---- |
| 800 |
<1> create a map which will hold the model types |
| 801 |
<2> declare the type of the `pages` variables (note the use of a string for the type) |
| 802 |
<3> use `createTypeCheckedModelTemplate` instead of `createTemplate` |
| 803 |
|
| 804 |
This time, when the template is compiled at the last line, an error occurs: |
| 805 |
|
| 806 |
.Template compilation time error |
| 807 |
---- |
| 808 |
[Static type checking] - No such property: text for class: Page |
| 809 |
---- |
| 810 |
|
| 811 |
This means that you don't need to wait for the page to be rendered to see an error. The use of `createTypeCheckedModelTemplate` is mandatory. |
| 812 |
|
| 813 |
=== Alternative declaration of types |
| 814 |
|
| 815 |
Alternatively, if the developer is also the one who writes the templates, it is possible to declare the types of the expected variables |
| 816 |
directly in the template. In this case, even if you call `createTemplate`, it will be type checked: |
| 817 |
|
| 818 |
[source,groovy] |
| 819 |
.Inline declaration of types |
| 820 |
---- |
| 821 |
include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/MarkupTemplateEngineSpecTest.groovy[tags=typechecked_inlined_template,indent=0] |
| 822 |
---- |
| 823 |
<1> types need to be declared in the `modelTypes` header |
| 824 |
<2> declare one variable per object in the model |
| 825 |
|
| 826 |
=== Performance of type checked templates |
| 827 |
|
| 828 |
An additional interest of using type checked models is that performance should improve. By telling the type checker what are the expected types, |
| 829 |
you also let the compiler generate optimized code for that, so if you are looking for the best performance, consider using type checked templates. |
| 830 |
|