Debian Codegraph @

subprojects/groovy-templates/src/spec/doc/template-engines.adoc

raw
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 = Template engines
23
24 == Introduction
25
26 Groovy supports multiple ways to generate text dynamically including `GStrings`, `printf` and <<_markupbuilder, MarkupBuilder>> just to name a few. In addition to these, there is a dedicated template framework which is well-suited to applications where the text to be generated follows the form of a static template.
27
28 == Template framework
29
30 The template framework in Groovy consists of a `TemplateEngine` abstract base class that engines must implement and a `Template` interface that the resulting templates they generate must implement.
31
32 Included with Groovy are several template engines:
33
34 - `SimpleTemplateEngine` - for basic templates
35 - `StreamingTemplateEngine` - functionally equivalent to `SimpleTemplateEngine`, but can handle strings larger than 64k
36 - `GStringTemplateEngine` - stores the template as writeable closures (useful for streaming scenarios)
37 - `XmlTemplateEngine` - works well when the template and output are valid XML
38 - `MarkupTemplateEngine` - a very complete, optimized, template engine
39
40 == SimpleTemplateEngine
41
42 Shown here is the `SimpleTemplateEngine` that allows you to use JSP-like scriptlets (see example below), script, and EL expressions in your template in order to generate parametrized text. Here is an example of using the system:
43
44 [source,groovy]
45 ----
46 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine1,indent=0]
47 ----
48
49 While it is generally not deemed good practice to mix processing logic in your template (or view), sometimes very simple logic can be useful. E.g. in the example above, we could change this:
50
51 [source,groovy]
52 ----
53 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine2,indent=0]
54 ----
55
56 to this (assuming we have set up a static import for capitalize **inside** the template):
57
58 [source,groovy]
59 ----
60 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine3,indent=0]
61 ----
62
63 or this:
64
65 [source,groovy]
66 ----
67 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine4,indent=0]
68 ----
69
70 to this:
71
72 [source,groovy]
73 ----
74 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine5,indent=0]
75 ----
76
77 === Advanced Usage Note
78
79 If you happen to be embedding your template directly in your script (as we did above) you have to be careful about backslash escaping. Because the template string itself will be parsed by Groovy before it is passed to the the templating framework, you have to escape any backslashes inside GString expressions or scriptlet 'code' that are entered as part of a Groovy program. E.g. if we wanted quotes around __The Big Apple__ above, we would use:
80
81 [source,groovy]
82 ----
83 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine6,indent=0]
84 ----
85
86 Similarly, if we wanted a newline, we would use:
87
88 [source,groovy]
89 ----
90 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine7,indent=0]
91 ----
92
93 in any GString expression or scriptlet $$'code'$$ that appears inside a Groovy script. A normal "`\n`" is fine within the static template text itself or if the entire template itself is in an external template file. Similarly, to represent an actual backslash in your text you would need
94
95 [source,groovy]
96 ----
97 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine8,indent=0]
98 ----
99
100 in an external file or
101
102 [source,groovy]
103 ----
104 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=simple_template_engine8,indent=0]
105 ----
106
107 in any GString expression or scriptlet $$'code'$$. (Note: the necessity to have this extra slash may go away in a future version of Groovy if we can find an easy way to support such a change.)
108
109 == StreamingTemplateEngine
110
111 The `StreamingTemplateEngine` engine is functionally equivalent to the `SimpleTemplateEngine`, but creates the template using writable closures making it more scalable for large templates. Specifically this template engine can handle strings larger than 64k.
112
113 It uses JSP style <% %> script and <%= %> expression syntax or GString style expressions. The variable 'out' is bound to the writer that the template is being written to.
114
115 Frequently, the template source will be a file but here we show a simple example providing the template as a string:
116
117 [source,groovy]
118 ----
119 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=streaming_template_engine,indent=0]
120 ----
121
122 == GStringTemplateEngine
123
124 As an example of using the `GStringTemplateEngine`, here is the example above done again (with a few changes to show some other options). First we will store the template in a file this time:
125
126 [source,groovy]
127 .test.template
128 ----
129 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=gstring_template_engine1,indent=0]
130 ----
131
132 Note that we used `out` instead of `print` to support the streaming nature of `GStringTemplateEngine`. Because we have the template in a separate file, there is no need to escape the backslashes. Here is how we call it:
133
134 [source,groovy]
135 ----
136 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=gstring_template_engine2,indent=0]
137 ----
138
139 and here is the output:
140
141 ----
142 Dear "Sam Pullara",
143 So nice to meet you in "The Big Apple".
144 See you in December,
145 Groovy-Dev
146 ----
147
148 == XmlTemplateEngine
149
150 `XmlTemplateEngine` for use in templating scenarios where both the template source and the expected output are intended to be XML. Templates may use the normal `${expression}` and `$variable` notations to insert an arbitrary expression into the template. In addition, support is also provided for special tags: `<gsp:scriptlet>` (for inserting code fragments) and `<gsp:expression>` (for code fragments which produce output).
151
152 Comments and processing instructions will be removed as part of processing and special XML characters such as `<`, `>`, `"` and `'` will be escaped using the respective XML notation. The output will also be indented using standard XML pretty printing.
153
154 The xmlns namespace definition for gsp: tags will be removed but other namespace definitions will be preserved (but may change to an equivalent position within the XML tree).
155
156 Normally, the template source will be in a file but here is a simple example providing the XML template as a string:
157
158 [source,groovy]
159 ----
160 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy[tags=xml_template_engine,indent=0]
161 ----
162
163 This example will produce this output:
164
165 [source,xml]
166 ----
167 <document type='letter'>
168 Dearest
169 <foo:to xmlns:foo='baz'>
170 Jochen &quot;blackdrag&quot; Theodorou
171 </foo:to>
172 How are you today?
173 </document>
174 ----
175
176 include::{rootProjectDir}/subprojects/groovy-templates/src/spec/doc/markup-template-engine.adoc[leveloffset=+1]
177
178 == Other solutions
179
180 Also, there are other templating solutions that can be used along with Groovy, such as http://freemarker.org[FreeMarker], http://velocity.apache.org[Velocity], http://stringtemplate.org[StringTemplate] and others.