1 |
747 |
jeremybenn |
// Copyright 2011 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
/*
|
6 |
|
|
Package template implements data-driven templates for generating textual output
|
7 |
|
|
such as HTML.
|
8 |
|
|
|
9 |
|
|
Templates are executed by applying them to a data structure. Annotations in the
|
10 |
|
|
template refer to elements of the data structure (typically a field of a struct
|
11 |
|
|
or a key in a map) to control execution and derive values to be displayed.
|
12 |
|
|
Execution of the template walks the structure and sets the cursor, represented
|
13 |
|
|
by a period '.' and called "dot", to the value at the current location in the
|
14 |
|
|
structure as execution proceeds.
|
15 |
|
|
|
16 |
|
|
The input text for a template is UTF-8-encoded text in any format.
|
17 |
|
|
"Actions"--data evaluations or control structures--are delimited by
|
18 |
|
|
"{{" and "}}"; all text outside actions is copied to the output unchanged.
|
19 |
|
|
Actions may not span newlines, although comments can.
|
20 |
|
|
|
21 |
|
|
Once constructed, a template may be executed safely in parallel.
|
22 |
|
|
|
23 |
|
|
Actions
|
24 |
|
|
|
25 |
|
|
Here is the list of actions. "Arguments" and "pipelines" are evaluations of
|
26 |
|
|
data, defined in detail below.
|
27 |
|
|
|
28 |
|
|
*/
|
29 |
|
|
// {{/* a comment */}}
|
30 |
|
|
// A comment; discarded. May contain newlines.
|
31 |
|
|
// Comments do not nest.
|
32 |
|
|
/*
|
33 |
|
|
|
34 |
|
|
{{pipeline}}
|
35 |
|
|
The default textual representation of the value of the pipeline
|
36 |
|
|
is copied to the output.
|
37 |
|
|
|
38 |
|
|
{{if pipeline}} T1 {{end}}
|
39 |
|
|
If the value of the pipeline is empty, no output is generated;
|
40 |
|
|
otherwise, T1 is executed. The empty values are false, 0, any
|
41 |
|
|
nil pointer or interface value, and any array, slice, map, or
|
42 |
|
|
string of length zero.
|
43 |
|
|
Dot is unaffected.
|
44 |
|
|
|
45 |
|
|
{{if pipeline}} T1 {{else}} T0 {{end}}
|
46 |
|
|
If the value of the pipeline is empty, T0 is executed;
|
47 |
|
|
otherwise, T1 is executed. Dot is unaffected.
|
48 |
|
|
|
49 |
|
|
{{range pipeline}} T1 {{end}}
|
50 |
|
|
The value of the pipeline must be an array, slice, or map. If
|
51 |
|
|
the value of the pipeline has length zero, nothing is output;
|
52 |
|
|
otherwise, dot is set to the successive elements of the array,
|
53 |
|
|
slice, or map and T1 is executed. If the value is a map and the
|
54 |
|
|
keys are of basic type with a defined order ("comparable"), the
|
55 |
|
|
elements will be visited in sorted key order.
|
56 |
|
|
|
57 |
|
|
{{range pipeline}} T1 {{else}} T0 {{end}}
|
58 |
|
|
The value of the pipeline must be an array, slice, or map. If
|
59 |
|
|
the value of the pipeline has length zero, dot is unaffected and
|
60 |
|
|
T0 is executed; otherwise, dot is set to the successive elements
|
61 |
|
|
of the array, slice, or map and T1 is executed.
|
62 |
|
|
|
63 |
|
|
{{template "name"}}
|
64 |
|
|
The template with the specified name is executed with nil data.
|
65 |
|
|
|
66 |
|
|
{{template "name" pipeline}}
|
67 |
|
|
The template with the specified name is executed with dot set
|
68 |
|
|
to the value of the pipeline.
|
69 |
|
|
|
70 |
|
|
{{with pipeline}} T1 {{end}}
|
71 |
|
|
If the value of the pipeline is empty, no output is generated;
|
72 |
|
|
otherwise, dot is set to the value of the pipeline and T1 is
|
73 |
|
|
executed.
|
74 |
|
|
|
75 |
|
|
{{with pipeline}} T1 {{else}} T0 {{end}}
|
76 |
|
|
If the value of the pipeline is empty, dot is unaffected and T0
|
77 |
|
|
is executed; otherwise, dot is set to the value of the pipeline
|
78 |
|
|
and T1 is executed.
|
79 |
|
|
|
80 |
|
|
Arguments
|
81 |
|
|
|
82 |
|
|
An argument is a simple value, denoted by one of the following.
|
83 |
|
|
|
84 |
|
|
- A boolean, string, character, integer, floating-point, imaginary
|
85 |
|
|
or complex constant in Go syntax. These behave like Go's untyped
|
86 |
|
|
constants, although raw strings may not span newlines.
|
87 |
|
|
- The character '.' (period):
|
88 |
|
|
.
|
89 |
|
|
The result is the value of dot.
|
90 |
|
|
- A variable name, which is a (possibly empty) alphanumeric string
|
91 |
|
|
preceded by a dollar sign, such as
|
92 |
|
|
$piOver2
|
93 |
|
|
or
|
94 |
|
|
$
|
95 |
|
|
The result is the value of the variable.
|
96 |
|
|
Variables are described below.
|
97 |
|
|
- The name of a field of the data, which must be a struct, preceded
|
98 |
|
|
by a period, such as
|
99 |
|
|
.Field
|
100 |
|
|
The result is the value of the field. Field invocations may be
|
101 |
|
|
chained:
|
102 |
|
|
.Field1.Field2
|
103 |
|
|
Fields can also be evaluated on variables, including chaining:
|
104 |
|
|
$x.Field1.Field2
|
105 |
|
|
- The name of a key of the data, which must be a map, preceded
|
106 |
|
|
by a period, such as
|
107 |
|
|
.Key
|
108 |
|
|
The result is the map element value indexed by the key.
|
109 |
|
|
Key invocations may be chained and combined with fields to any
|
110 |
|
|
depth:
|
111 |
|
|
.Field1.Key1.Field2.Key2
|
112 |
|
|
Although the key must be an alphanumeric identifier, unlike with
|
113 |
|
|
field names they do not need to start with an upper case letter.
|
114 |
|
|
Keys can also be evaluated on variables, including chaining:
|
115 |
|
|
$x.key1.key2
|
116 |
|
|
- The name of a niladic method of the data, preceded by a period,
|
117 |
|
|
such as
|
118 |
|
|
.Method
|
119 |
|
|
The result is the value of invoking the method with dot as the
|
120 |
|
|
receiver, dot.Method(). Such a method must have one return value (of
|
121 |
|
|
any type) or two return values, the second of which is an error.
|
122 |
|
|
If it has two and the returned error is non-nil, execution terminates
|
123 |
|
|
and an error is returned to the caller as the value of Execute.
|
124 |
|
|
Method invocations may be chained and combined with fields and keys
|
125 |
|
|
to any depth:
|
126 |
|
|
.Field1.Key1.Method1.Field2.Key2.Method2
|
127 |
|
|
Methods can also be evaluated on variables, including chaining:
|
128 |
|
|
$x.Method1.Field
|
129 |
|
|
- The name of a niladic function, such as
|
130 |
|
|
fun
|
131 |
|
|
The result is the value of invoking the function, fun(). The return
|
132 |
|
|
types and values behave as in methods. Functions and function
|
133 |
|
|
names are described below.
|
134 |
|
|
|
135 |
|
|
Arguments may evaluate to any type; if they are pointers the implementation
|
136 |
|
|
automatically indirects to the base type when required.
|
137 |
|
|
|
138 |
|
|
A pipeline is a possibly chained sequence of "commands". A command is a simple
|
139 |
|
|
value (argument) or a function or method call, possibly with multiple arguments:
|
140 |
|
|
|
141 |
|
|
Argument
|
142 |
|
|
The result is the value of evaluating the argument.
|
143 |
|
|
.Method [Argument...]
|
144 |
|
|
The method can be alone or the last element of a chain but,
|
145 |
|
|
unlike methods in the middle of a chain, it can take arguments.
|
146 |
|
|
The result is the value of calling the method with the
|
147 |
|
|
arguments:
|
148 |
|
|
dot.Method(Argument1, etc.)
|
149 |
|
|
functionName [Argument...]
|
150 |
|
|
The result is the value of calling the function associated
|
151 |
|
|
with the name:
|
152 |
|
|
function(Argument1, etc.)
|
153 |
|
|
Functions and function names are described below.
|
154 |
|
|
|
155 |
|
|
Pipelines
|
156 |
|
|
|
157 |
|
|
A pipeline may be "chained" by separating a sequence of commands with pipeline
|
158 |
|
|
characters '|'. In a chained pipeline, the result of the each command is
|
159 |
|
|
passed as the last argument of the following command. The output of the final
|
160 |
|
|
command in the pipeline is the value of the pipeline.
|
161 |
|
|
|
162 |
|
|
The output of a command will be either one value or two values, the second of
|
163 |
|
|
which has type error. If that second value is present and evaluates to
|
164 |
|
|
non-nil, execution terminates and the error is returned to the caller of
|
165 |
|
|
Execute.
|
166 |
|
|
|
167 |
|
|
Variables
|
168 |
|
|
|
169 |
|
|
A pipeline inside an action may initialize a variable to capture the result.
|
170 |
|
|
The initialization has syntax
|
171 |
|
|
|
172 |
|
|
$variable := pipeline
|
173 |
|
|
|
174 |
|
|
where $variable is the name of the variable. An action that declares a
|
175 |
|
|
variable produces no output.
|
176 |
|
|
|
177 |
|
|
If a "range" action initializes a variable, the variable is set to the
|
178 |
|
|
successive elements of the iteration. Also, a "range" may declare two
|
179 |
|
|
variables, separated by a comma:
|
180 |
|
|
|
181 |
|
|
$index, $element := pipeline
|
182 |
|
|
|
183 |
|
|
in which case $index and $element are set to the successive values of the
|
184 |
|
|
array/slice index or map key and element, respectively. Note that if there is
|
185 |
|
|
only one variable, it is assigned the element; this is opposite to the
|
186 |
|
|
convention in Go range clauses.
|
187 |
|
|
|
188 |
|
|
A variable's scope extends to the "end" action of the control structure ("if",
|
189 |
|
|
"with", or "range") in which it is declared, or to the end of the template if
|
190 |
|
|
there is no such control structure. A template invocation does not inherit
|
191 |
|
|
variables from the point of its invocation.
|
192 |
|
|
|
193 |
|
|
When execution begins, $ is set to the data argument passed to Execute, that is,
|
194 |
|
|
to the starting value of dot.
|
195 |
|
|
|
196 |
|
|
Examples
|
197 |
|
|
|
198 |
|
|
Here are some example one-line templates demonstrating pipelines and variables.
|
199 |
|
|
All produce the quoted word "output":
|
200 |
|
|
|
201 |
|
|
{{"\"output\""}}
|
202 |
|
|
A string constant.
|
203 |
|
|
{{`"output"`}}
|
204 |
|
|
A raw string constant.
|
205 |
|
|
{{printf "%q" "output"}}
|
206 |
|
|
A function call.
|
207 |
|
|
{{"output" | printf "%q"}}
|
208 |
|
|
A function call whose final argument comes from the previous
|
209 |
|
|
command.
|
210 |
|
|
{{"put" | printf "%s%s" "out" | printf "%q"}}
|
211 |
|
|
A more elaborate call.
|
212 |
|
|
{{"output" | printf "%s" | printf "%q"}}
|
213 |
|
|
A longer chain.
|
214 |
|
|
{{with "output"}}{{printf "%q" .}}{{end}}
|
215 |
|
|
A with action using dot.
|
216 |
|
|
{{with $x := "output" | printf "%q"}}{{$x}}{{end}}
|
217 |
|
|
A with action that creates and uses a variable.
|
218 |
|
|
{{with $x := "output"}}{{printf "%q" $x}}{{end}}
|
219 |
|
|
A with action that uses the variable in another action.
|
220 |
|
|
{{with $x := "output"}}{{$x | printf "%q"}}{{end}}
|
221 |
|
|
The same, but pipelined.
|
222 |
|
|
|
223 |
|
|
Functions
|
224 |
|
|
|
225 |
|
|
During execution functions are found in two function maps: first in the
|
226 |
|
|
template, then in the global function map. By default, no functions are defined
|
227 |
|
|
in the template but the Funcs methods can be used to add them.
|
228 |
|
|
|
229 |
|
|
Predefined global functions are named as follows.
|
230 |
|
|
|
231 |
|
|
and
|
232 |
|
|
Returns the boolean AND of its arguments by returning the
|
233 |
|
|
first empty argument or the last argument, that is,
|
234 |
|
|
"and x y" behaves as "if x then y else x". All the
|
235 |
|
|
arguments are evaluated.
|
236 |
|
|
html
|
237 |
|
|
Returns the escaped HTML equivalent of the textual
|
238 |
|
|
representation of its arguments.
|
239 |
|
|
index
|
240 |
|
|
Returns the result of indexing its first argument by the
|
241 |
|
|
following arguments. Thus "index x 1 2 3" is, in Go syntax,
|
242 |
|
|
x[1][2][3]. Each indexed item must be a map, slice, or array.
|
243 |
|
|
js
|
244 |
|
|
Returns the escaped JavaScript equivalent of the textual
|
245 |
|
|
representation of its arguments.
|
246 |
|
|
len
|
247 |
|
|
Returns the integer length of its argument.
|
248 |
|
|
not
|
249 |
|
|
Returns the boolean negation of its single argument.
|
250 |
|
|
or
|
251 |
|
|
Returns the boolean OR of its arguments by returning the
|
252 |
|
|
first non-empty argument or the last argument, that is,
|
253 |
|
|
"or x y" behaves as "if x then x else y". All the
|
254 |
|
|
arguments are evaluated.
|
255 |
|
|
print
|
256 |
|
|
An alias for fmt.Sprint
|
257 |
|
|
printf
|
258 |
|
|
An alias for fmt.Sprintf
|
259 |
|
|
println
|
260 |
|
|
An alias for fmt.Sprintln
|
261 |
|
|
urlquery
|
262 |
|
|
Returns the escaped value of the textual representation of
|
263 |
|
|
its arguments in a form suitable for embedding in a URL query.
|
264 |
|
|
|
265 |
|
|
The boolean functions take any zero value to be false and a non-zero value to
|
266 |
|
|
be true.
|
267 |
|
|
|
268 |
|
|
Associated templates
|
269 |
|
|
|
270 |
|
|
Each template is named by a string specified when it is created. Also, each
|
271 |
|
|
template is associated with zero or more other templates that it may invoke by
|
272 |
|
|
name; such associations are transitive and form a name space of templates.
|
273 |
|
|
|
274 |
|
|
A template may use a template invocation to instantiate another associated
|
275 |
|
|
template; see the explanation of the "template" action above. The name must be
|
276 |
|
|
that of a template associated with the template that contains the invocation.
|
277 |
|
|
|
278 |
|
|
Nested template definitions
|
279 |
|
|
|
280 |
|
|
When parsing a template, another template may be defined and associated with the
|
281 |
|
|
template being parsed. Template definitions must appear at the top level of the
|
282 |
|
|
template, much like global variables in a Go program.
|
283 |
|
|
|
284 |
|
|
The syntax of such definitions is to surround each template declaration with a
|
285 |
|
|
"define" and "end" action.
|
286 |
|
|
|
287 |
|
|
The define action names the template being created by providing a string
|
288 |
|
|
constant. Here is a simple example:
|
289 |
|
|
|
290 |
|
|
`{{define "T1"}}ONE{{end}}
|
291 |
|
|
{{define "T2"}}TWO{{end}}
|
292 |
|
|
{{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
|
293 |
|
|
{{template "T3"}}`
|
294 |
|
|
|
295 |
|
|
This defines two templates, T1 and T2, and a third T3 that invokes the other two
|
296 |
|
|
when it is executed. Finally it invokes T3. If executed this template will
|
297 |
|
|
produce the text
|
298 |
|
|
|
299 |
|
|
ONE TWO
|
300 |
|
|
|
301 |
|
|
By construction, a template may reside in only one association. If it's
|
302 |
|
|
necessary to have a template addressable from multiple associations, the
|
303 |
|
|
template definition must be parsed multiple times to create distinct *Template
|
304 |
|
|
values.
|
305 |
|
|
|
306 |
|
|
Parse may be called multiple times to assemble the various associated templates;
|
307 |
|
|
see the ParseFiles and ParseGlob functions and methods for simple ways to parse
|
308 |
|
|
related templates stored in files.
|
309 |
|
|
|
310 |
|
|
A template may be executed directly or through ExecuteTemplate, which executes
|
311 |
|
|
an associated template identified by name. To invoke our example above, we
|
312 |
|
|
might write,
|
313 |
|
|
|
314 |
|
|
err := tmpl.Execute(os.Stdout, "no data needed")
|
315 |
|
|
if err != nil {
|
316 |
|
|
log.Fatalf("execution failed: %s", err)
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
or to invoke a particular template explicitly by name,
|
320 |
|
|
|
321 |
|
|
err := tmpl.ExecuteTemplate(os.Stdout, "T2", "no data needed")
|
322 |
|
|
if err != nil {
|
323 |
|
|
log.Fatalf("execution failed: %s", err)
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
*/
|
327 |
|
|
package template
|