Project

General

Profile

TemplateParser » History » Version 47

Elmer de Looff, 2012-07-31 11:42
Better explanation of reloading and auto-loading

1 1 Elmer de Looff
h1. TemplateParser
2 25 Elmer de Looff
3 25 Elmer de Looff
{{>toc}}
4 1 Elmer de Looff
5 1 Elmer de Looff
The µWeb TemplateParser is a in-house developed templating engine that provides tag replacement, tag-functions and template control functions. This document will describe the following:
6 38 Elmer de Looff
* *[[TemplateParser#using|Using TemplateParser]]* inside a µWeb PageMaker
7 21 Elmer de Looff
* The *[[TemplateParser#template|Template class]]*, used to parse the templating language
8 21 Elmer de Looff
* The *[[TemplateParser#parser|Parser class]]*, which provides template loading and caching
9 21 Elmer de Looff
* *[[TemplateParser#syntax|Template syntax]]*, an overview of the language's constructs and behaviors
10 1 Elmer de Looff
11 1 Elmer de Looff
First though, to help with understanding the TemplateParser, a minimal size template document:
12 1 Elmer de Looff
13 1 Elmer de Looff
<pre><code class="html">
14 1 Elmer de Looff
Hello [title] [name]
15 1 Elmer de Looff
</code></pre>
16 4 Elmer de Looff
17 1 Elmer de Looff
The above document contains two simple template tags. These tags are delimited by square brackets, and they will be replaced by the named argument provided during parsing. If this name is not present, then the literal presentation of the tag will remain in the output.
18 1 Elmer de Looff
19 38 Elmer de Looff
h1(#using). Using TemplateParser inside µWeb
20 38 Elmer de Looff
21 46 Elmer de Looff
Within the default µWeb [[PageMaker]], there is a @parser@ property, which provides a [[TemplateParser#parser|Parser]] object. The class constant @TEMPLATE_DIR@ provides the template search directory. The default template directory is @'templates'@. *N.B.* This path is relative to the file that contains the PageMaker class.
22 38 Elmer de Looff
23 38 Elmer de Looff
An example of TemplateParser to create a complete response:
24 38 Elmer de Looff
<pre><code class="python">
25 38 Elmer de Looff
import uweb
26 38 Elmer de Looff
import time
27 38 Elmer de Looff
28 38 Elmer de Looff
class PageMaker(uweb.PageMaker):
29 38 Elmer de Looff
  def VersionPage(self):
30 38 Elmer de Looff
    return self.parser.Parse(
31 38 Elmer de Looff
      'version.utp', year=time.strftime('%Y'), version=uweb.__version__)
32 38 Elmer de Looff
</code></pre>
33 38 Elmer de Looff
34 38 Elmer de Looff
The example template for the above file could look something like this:
35 38 Elmer de Looff
36 38 Elmer de Looff
<pre><code class="html">
37 38 Elmer de Looff
<!DOCTYPE html>
38 38 Elmer de Looff
<html>
39 38 Elmer de Looff
  <head>
40 38 Elmer de Looff
    <title>µWeb version info</title>
41 38 Elmer de Looff
  </head>
42 38 Elmer de Looff
  <body>
43 38 Elmer de Looff
    <p>µWeb version [version] - Copyright 2010-[year] Underdark</p>
44 38 Elmer de Looff
  </body>
45 38 Elmer de Looff
</html>
46 38 Elmer de Looff
</code></pre>
47 38 Elmer de Looff
48 38 Elmer de Looff
And would result in the following output:
49 38 Elmer de Looff
50 38 Elmer de Looff
<pre><code class="html">
51 38 Elmer de Looff
<!DOCTYPE html>
52 38 Elmer de Looff
<html>
53 38 Elmer de Looff
  <head>
54 38 Elmer de Looff
    <title>µWeb version info</title>
55 38 Elmer de Looff
  </head>
56 38 Elmer de Looff
  <body>
57 38 Elmer de Looff
    <p>µWeb version 0.12 - Copyright 2010-2012 Underdark</p>
58 38 Elmer de Looff
  </body>
59 38 Elmer de Looff
</html>
60 38 Elmer de Looff
</code></pre>
61 38 Elmer de Looff
62 38 Elmer de Looff
With these initial small demonstrations behind us, let's explore the @TemplateParser@ further
63 38 Elmer de Looff
64 5 Elmer de Looff
h1(#template). Template class
65 4 Elmer de Looff
66 4 Elmer de Looff
The @Template@ class provides the interface for pre-parsing templates, loading them from files and parsing single templates to completion. During pre-parsing, constructs such as loops and conditional statements are converted to @TemplateLoop@ and @TemplateConditional@ objects, and their scopes nested appropriately in the @Template@. Tags are replaced by @TemplateTag@ instances, and text is captured in @TemplateText@. All of these provide @Parse@ methods, which together result in the combined parsed template output.
67 4 Elmer de Looff
68 4 Elmer de Looff
h2. Creating a template
69 4 Elmer de Looff
70 4 Elmer de Looff
A template is created simple by providing a string input to the @Template@'s constructor. This will return a valid Template instance (or raise an error if there is a problem with the [[TemplateParser#syntax|syntax]]:
71 4 Elmer de Looff
72 4 Elmer de Looff
<pre><code class="python">
73 21 Elmer de Looff
>>> import templateparser
74 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
75 4 Elmer de Looff
>>> template
76 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
77 4 Elmer de Looff
</code></pre>
78 4 Elmer de Looff
79 4 Elmer de Looff
Above can be seen the various parts of the template, which will be combined to output once parsed.
80 4 Elmer de Looff
81 4 Elmer de Looff
h2. Loading a template from file
82 4 Elmer de Looff
83 4 Elmer de Looff
The @Template@ class provides a @classmethod@ called @FromFile@, which loads the template at the path.
84 4 Elmer de Looff
85 4 Elmer de Looff
Loading a template named @example.utp@ from the current working directory:
86 4 Elmer de Looff
87 4 Elmer de Looff
<pre><code class="python">
88 21 Elmer de Looff
>>> import templateparser
89 4 Elmer de Looff
>>> template = templateparser.Template.FromFile('example.utp')
90 4 Elmer de Looff
>>> template
91 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
92 4 Elmer de Looff
</code></pre>
93 4 Elmer de Looff
94 5 Elmer de Looff
h2. Parsing a template
95 4 Elmer de Looff
96 4 Elmer de Looff
Parsing a template can be done by calling the @Template@'s @Parse@ method. The keyword arguments provided to this call will from the replacement mapping for the template. In the following example, we will provide one such keyword, and leave the other undefined to show the (basic) behavior of the @Template.Parse@ method.
97 4 Elmer de Looff
98 4 Elmer de Looff
<pre><code class="python">
99 21 Elmer de Looff
>>> import templateparser
100 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
101 8 Elmer de Looff
>>> template.Parse(title='sir')
102 8 Elmer de Looff
'Hello sir [name]'
103 4 Elmer de Looff
</code></pre>
104 1 Elmer de Looff
105 1 Elmer de Looff
h1(#parser). Parser class
106 6 Elmer de Looff
107 1 Elmer de Looff
The @Parser@ class provides simple management of multiple @Template@ objects. It is mainly used to load templates from disk. When initiating a @Parser@, the first argument provides the search path from where templates should be loaded (the default is the current working directory). An optional second argument can be provided to preload the template cache: a mapping of names and @Template@ objects.
108 1 Elmer de Looff
109 8 Elmer de Looff
h2. Loading templates
110 8 Elmer de Looff
111 6 Elmer de Looff
Creating a parser object, and loading the 'example.utp' template from the 'templates' directory works like this:
112 6 Elmer de Looff
113 6 Elmer de Looff
<pre><code class="python">
114 21 Elmer de Looff
>>> import templateparser
115 7 Elmer de Looff
>>> # This sets the 'templates' directory as the search path for AddTemplate
116 7 Elmer de Looff
>>> parser = templateparser.Parser('templates')
117 7 Elmer de Looff
>>> # Loads the 'templates/example.utp' and stores it as 'example.utp':
118 26 Elmer de Looff
>>> parser.AddTemplate('example.utp')
119 27 Elmer de Looff
>>> parser.Parse('example.utp', title='mister', name='Bob Dobalina')
120 1 Elmer de Looff
'Hello mister Bob Dobalina'
121 6 Elmer de Looff
</code></pre>
122 1 Elmer de Looff
123 29 Elmer de Looff
The @AddTemplate@ method takes a second optional argument, which allows us to give the template a different name in the cache:
124 29 Elmer de Looff
125 29 Elmer de Looff
<pre><code class="python">
126 29 Elmer de Looff
>>> parser = templateparser.Parser('templates')
127 30 Elmer de Looff
>>> parser.AddTemplate('example.utp', name='greeting')
128 29 Elmer de Looff
>>> parser.Parse('greeting', title='mister', name='Bob Dobalina')
129 29 Elmer de Looff
'Hello mister Bob Dobalina'
130 29 Elmer de Looff
</code></pre>
131 29 Elmer de Looff
132 29 Elmer de Looff
As you can see, the name of the template in the cache is not necessarily the same as the one on disk. Often though, this is not necessary to change, so @AddTemplate@ need only be called with one argument. Or not at all, as the following section will show.
133 1 Elmer de Looff
134 47 Elmer de Looff
h2. Template cache, reloading, and auto-loading
135 8 Elmer de Looff
136 47 Elmer de Looff
The @Parser@ at heart is a dictionary that maps the names of templates to @Template@ instances. When they are loaded from disk they are pre-parsed, checked and cached. Subsequent uses of the same template will therefore be faster, as the initial parsing will not have to be repeated.
137 1 Elmer de Looff
138 47 Elmer de Looff
Templates loaded from a file keep track of the modification time (@mtime@) of the originating template file. Upon each parse, the source file is checked, and if the modification time is newer than when it was loaded, the template is read from disk and then parsed. This way, templates are never out of date.
139 47 Elmer de Looff
140 47 Elmer de Looff
Whenever the @Parser@ is requested to @Parse@ or return a template that it doesn't have loaded already, the auto-loading mechanism triggers. This searches for the given template name in the configured template directory. If a filename matches (exactly), it is automatically loaded and used to fulfill the Parse request. If no matching file is found, an error is triggered.
141 47 Elmer de Looff
142 47 Elmer de Looff
Below follows an example of auto-loading:
143 47 Elmer de Looff
144 8 Elmer de Looff
<pre><code class="python">
145 1 Elmer de Looff
>>> import templateparser
146 8 Elmer de Looff
>>> parser = templateparser.Parser('templates')
147 6 Elmer de Looff
>>> 'example.utp' in parser
148 47 Elmer de Looff
False       # Since we haven't loaded it, the template is not in the parser storage
149 10 Elmer de Looff
>>> parser
150 10 Elmer de Looff
Parser({})  # The parser is empty (has no cached templates)
151 10 Elmer de Looff
</code></pre>
152 1 Elmer de Looff
153 10 Elmer de Looff
Attempting to parse a template that doesn't exist in the parser cache triggers an automatic load:
154 10 Elmer de Looff
155 31 Jan Klopper
<pre><code class="python">
156 47 Elmer de Looff
>>> parser.Parse('example.utp', title='mister', name='Bob Dobalina')
157 10 Elmer de Looff
'Hello mister Bob Dobalina'
158 10 Elmer de Looff
>>> 'example.utp' in parser
159 10 Elmer de Looff
True
160 10 Elmer de Looff
>>> parser
161 10 Elmer de Looff
Parser({'example.utp': Template([TemplateText('Hello '), TemplateTag('[title]'),
162 10 Elmer de Looff
                                 TemplateText(' '), TemplateTag('[name]')])})
163 10 Elmer de Looff
</code></pre>
164 10 Elmer de Looff
165 1 Elmer de Looff
If these cannot be found, @TemplateReadError@ is raised:
166 10 Elmer de Looff
167 10 Elmer de Looff
<pre><code class="python">
168 10 Elmer de Looff
>>> import templateparser
169 10 Elmer de Looff
>>> parser = templateparser.Parser('templates')
170 47 Elmer de Looff
>>> parser.Parse('bad_template.utp', failure='imminent')
171 10 Elmer de Looff
Traceback (most recent call last):
172 10 Elmer de Looff
  File "<stdin>", line 1, in <module>
173 1 Elmer de Looff
  File "/var/lib/underdark/libs/uweb/templateparser.py", line 147, in __getitem__
174 37 Elmer de Looff
    self.AddTemplate(template)
175 37 Elmer de Looff
  File "/var/lib/underdark/libs/uweb/templateparser.py", line 171, in AddTemplate
176 37 Elmer de Looff
    raise TemplateReadError('Could not load template %r' % template_path)
177 37 Elmer de Looff
underdark.libs.uweb.templateparser.TemplateReadError: Could not load template 'templates/bad_template.utp'
178 37 Elmer de Looff
</code></pre>
179 37 Elmer de Looff
180 37 Elmer de Looff
h2. @Parse@ and @ParseString@ methods
181 37 Elmer de Looff
182 37 Elmer de Looff
For convencience and consistency, the @Parser@ comes with two handy methods to provide parsing of @Template@ objects, one from its cache, one from raw template strings. It is recommended to use these over the previously shown direct key-based access:
183 37 Elmer de Looff
184 37 Elmer de Looff
<pre><code class="python">
185 37 Elmer de Looff
>>> import templateparser
186 37 Elmer de Looff
>>> parser = templateparser.Parser('templates')
187 37 Elmer de Looff
>>> parser.Parse('example.utp', title='mister', name='Bob Dobalina')
188 10 Elmer de Looff
'Hello mister Bob Dobalina'
189 10 Elmer de Looff
>>> parser.ParseString('Hello [title] [name]', title='mister', name='Bob Dobalina')
190 10 Elmer de Looff
'Hello mister Bob Dobalina'</code></pre>
191 1 Elmer de Looff
192 5 Elmer de Looff
h1(#syntax). Templating language syntax
193 11 Elmer de Looff
194 11 Elmer de Looff
The templating syntax is relatively limited, but with the limited syntax it provides a flexible and rich system to create templates. Covered in these examples are:
195 11 Elmer de Looff
* Simple tags (used in various examples above)
196 11 Elmer de Looff
* Tag indexing
197 11 Elmer de Looff
* Tag functions
198 11 Elmer de Looff
* Template language constructs
199 11 Elmer de Looff
200 11 Elmer de Looff
All examples will consist of three parts:
201 11 Elmer de Looff
# The example template
202 11 Elmer de Looff
# The python invocation string (the template will be named 'example.utp')
203 11 Elmer de Looff
# The resulting output (as source, not as parsed HTML)
204 11 Elmer de Looff
205 11 Elmer de Looff
h2. Simple tags
206 11 Elmer de Looff
207 11 Elmer de Looff
This is an example for the most basic form of template tags. The tag is enclosed by square brackets as such: @[tag]@. Tags that match a provided argument to the Parse call get replaced. If there is no argument that matches the tag name, it is returned in the output verbatim. This is also demonstrated in the below example
208 11 Elmer de Looff
209 11 Elmer de Looff
The example below is a repeat of the example how to use TemplateParser inside µWeb, and shows the template result:
210 11 Elmer de Looff
211 11 Elmer de Looff
<pre><code class="html">
212 11 Elmer de Looff
<!DOCTYPE html>
213 11 Elmer de Looff
<html>
214 11 Elmer de Looff
  <head>
215 11 Elmer de Looff
    <title>µWeb version info</title>
216 11 Elmer de Looff
  </head>
217 11 Elmer de Looff
  <body>
218 11 Elmer de Looff
    <p>µWeb version [version] - Copyright 2010-[year] Underdark</p>
219 11 Elmer de Looff
    <p>
220 11 Elmer de Looff
      This [paragraph] is not replaced because there is no
221 11 Elmer de Looff
      paragraph argument provided to the parser.
222 11 Elmer de Looff
    </p>
223 11 Elmer de Looff
  </body>
224 11 Elmer de Looff
</html>
225 11 Elmer de Looff
</code></pre>
226 11 Elmer de Looff
227 11 Elmer de Looff
<pre><code class="python">
228 11 Elmer de Looff
>>> parser.Parse('version.utp', year=time.strftime('%Y'), version=uweb.__version__)
229 11 Elmer de Looff
</code></pre>
230 11 Elmer de Looff
231 11 Elmer de Looff
<pre><code class="html">
232 11 Elmer de Looff
<!DOCTYPE html>
233 11 Elmer de Looff
<html>
234 11 Elmer de Looff
  <head>
235 11 Elmer de Looff
    <title>µWeb version info</title>
236 11 Elmer de Looff
  </head>
237 11 Elmer de Looff
  <body>
238 11 Elmer de Looff
    <p>µWeb version 0.11 - Copyright 2010-212 Underdark</p>
239 11 Elmer de Looff
    <p>
240 11 Elmer de Looff
      This [paragraph] is not replaced because there is no
241 11 Elmer de Looff
      paragraph argument provided to the parser.
242 11 Elmer de Looff
    </p>
243 11 Elmer de Looff
  </body>
244 11 Elmer de Looff
</html>
245 11 Elmer de Looff
</code></pre>
246 11 Elmer de Looff
247 45 Elmer de Looff
h3. Valid tag name characters
248 42 Elmer de Looff
249 42 Elmer de Looff
Tag names are created from the same characters as valid Python variable names. This means they can contain upper and lower case letters, numbers and underscores. In regex terms, a tag should match @\w+@.
250 42 Elmer de Looff
251 42 Elmer de Looff
*N.B.:* Some names are illegal in Python as variable names but valid as tag names (tag names may start with a number). You can use these and pass the replacements as a dictionary using ** if you have a need for it.
252 42 Elmer de Looff
253 11 Elmer de Looff
h2. Tag indexing
254 11 Elmer de Looff
255 32 Elmer de Looff
In addition to simple (re)placement of strings using the @TemplateParser@, you can also provide it with a @list@, @dictionary@, or other indexable object, and from it, fetch various @indices@, @keys@ or @attributes@. The separation character between the _tagname_ and the _index_ is the _colon_ (":"):
256 32 Elmer de Looff
257 32 Elmer de Looff
258 32 Elmer de Looff
h3. List/tuple index addressing
259 32 Elmer de Looff
260 34 Elmer de Looff
This works for lists and tuples, but also for any other object that supports indexing. That is, every object that accepts integers on its @__getitem__@ method.
261 34 Elmer de Looff
262 32 Elmer de Looff
<pre><code class="html">
263 32 Elmer de Looff
This is [var:0] [var:1].
264 32 Elmer de Looff
</code></pre>
265 32 Elmer de Looff
266 32 Elmer de Looff
<pre><code class="python">
267 32 Elmer de Looff
>>> parser.Parse('message.utp', var=('delicious', 'spam'))
268 32 Elmer de Looff
</code></pre>
269 32 Elmer de Looff
270 32 Elmer de Looff
<pre><code class="html">
271 32 Elmer de Looff
This is delicious spam.
272 32 Elmer de Looff
</code></pre>
273 32 Elmer de Looff
274 33 Elmer de Looff
h3. Dictionary key addressing
275 32 Elmer de Looff
276 34 Elmer de Looff
This works for dictionaries, but also for any other object that behaves like a key-value mapping. That is, every object that accepts strings on its @__getitem__@ method.
277 34 Elmer de Looff
278 32 Elmer de Looff
<pre><code class="html">
279 32 Elmer de Looff
This is [var:adjective] [var:noun].
280 32 Elmer de Looff
</code></pre>
281 32 Elmer de Looff
282 32 Elmer de Looff
<pre><code class="python">
283 32 Elmer de Looff
>>> parser.Parse('message.utp', var={'adjective': 'delicious', 'noun': 'spam'})
284 32 Elmer de Looff
</code></pre>
285 32 Elmer de Looff
286 32 Elmer de Looff
<pre><code class="html">
287 32 Elmer de Looff
This is delicious spam.
288 32 Elmer de Looff
</code></pre>
289 32 Elmer de Looff
290 33 Elmer de Looff
h3. Attribute name addressing
291 32 Elmer de Looff
292 34 Elmer de Looff
This works for any object that has named attributes. If the attribute is a method, it will *not* be executed automatically, the return value will simply be the (un)bound method itself.
293 34 Elmer de Looff
294 32 Elmer de Looff
<pre><code class="html">
295 32 Elmer de Looff
This is [var:adjective] [var:noun].
296 32 Elmer de Looff
</code></pre>
297 32 Elmer de Looff
298 32 Elmer de Looff
<pre><code class="python">
299 32 Elmer de Looff
>>> class Struct(object):
300 32 Elmer de Looff
...   pass
301 32 Elmer de Looff
...
302 32 Elmer de Looff
>>> var = Struct()
303 32 Elmer de Looff
>>> var.adjective = 'delicious'
304 32 Elmer de Looff
>>> var.noun = 'spam'
305 32 Elmer de Looff
>>> parser.Parse('message.utp', var=var)
306 32 Elmer de Looff
</code></pre>
307 32 Elmer de Looff
308 32 Elmer de Looff
<pre><code class="html">
309 32 Elmer de Looff
This is delicious spam.
310 32 Elmer de Looff
</code></pre>
311 32 Elmer de Looff
312 33 Elmer de Looff
h3. Lookup order
313 32 Elmer de Looff
314 32 Elmer de Looff
For objects and constructs that provide multiple ways of looking up information, the lookup order can be very important. For any of the first three steps, if they are successful, the retrieved value is returned, and no further attempts are made:
315 32 Elmer de Looff
316 32 Elmer de Looff
# If the @needle@ is parseable as integer, it will first be used as an index. This will also work for mappings with numeric keys;
317 32 Elmer de Looff
# If the above fails, the @needle@ is assumed to be a string-like mapping key, and this is attempted
318 32 Elmer de Looff
# If the above fails, the @needle@ is used as an attribute name;
319 32 Elmer de Looff
# If all of the above fail, *@TemplateKeyError@* is raised, as the @needle@ could not be found on the object.
320 34 Elmer de Looff
321 34 Elmer de Looff
h3. Nested indexes
322 34 Elmer de Looff
323 34 Elmer de Looff
There may be cases where the value you need is not at the top-level index of an object. This is not a problem, since TemplateParser supports arbitrary-depth nested structures in its index-lookup:
324 34 Elmer de Looff
325 34 Elmer de Looff
<pre><code class="html">
326 34 Elmer de Looff
This is a variable from [some:levels:down:1].
327 34 Elmer de Looff
</code></pre>
328 34 Elmer de Looff
329 34 Elmer de Looff
<pre><code class="python">
330 34 Elmer de Looff
>>> class Struct(object):
331 34 Elmer de Looff
...   pass
332 34 Elmer de Looff
...
333 34 Elmer de Looff
>>> var = Struct()
334 34 Elmer de Looff
>>> var.levels = {'down': ('the sky', 'the depths')}
335 34 Elmer de Looff
>>> parser.Parse('message.utp', some=var)
336 34 Elmer de Looff
</code></pre>
337 34 Elmer de Looff
338 34 Elmer de Looff
<pre><code class="html">
339 34 Elmer de Looff
This is a variable from the depths.
340 34 Elmer de Looff
</code></pre>
341 16 Elmer de Looff
342 43 Elmer de Looff
h3. Valid index characters
343 43 Elmer de Looff
344 43 Elmer de Looff
Indexes may be constructed from upper and lower case letters, numbers, underscores and dashes. There are no restrictions on first character, only a minimum length of one. Regex-wise, they need to match @[\w-]+@
345 43 Elmer de Looff
346 11 Elmer de Looff
h2. Tag functions
347 11 Elmer de Looff
348 35 Elmer de Looff
Once you arrive at the tag/value you want, there's often some things that need to happen before the resulting template is sent to the requesting client (browser). HTML escaping is an obvious one, but url quoting of single arguments may also be helpful, as well as uppercasing, printing the length of a list (instead of the raw list) and various other uses.
349 15 Elmer de Looff
350 21 Elmer de Looff
h3. Default html escaping
351 1 Elmer de Looff
352 36 Elmer de Looff
Using a tag function is a fairly straightforward process, just add the name of the function after the tagname, separated by a pipe ( | ):
353 35 Elmer de Looff
354 36 Elmer de Looff
<pre><code class="html">
355 36 Elmer de Looff
And he said: [message|html]
356 36 Elmer de Looff
</code></pre>
357 36 Elmer de Looff
358 36 Elmer de Looff
<pre><code class="python">
359 36 Elmer de Looff
>>> parser.Parse('message.utp', message='"Hello"')
360 36 Elmer de Looff
</code></pre>
361 36 Elmer de Looff
362 36 Elmer de Looff
<pre><code class="html">
363 36 Elmer de Looff
And he said: &quot;Hello&quot;
364 36 Elmer de Looff
</code></pre>
365 36 Elmer de Looff
366 39 Elmer de Looff
Using the *html* tag function makes the tag value safe for printing in an HTML document. Because we believe this is _really_ important, the html escaping tag function is always applied when no other tag function is applied:
367 36 Elmer de Looff
368 36 Elmer de Looff
<pre><code class="html">
369 36 Elmer de Looff
And he said: [message]
370 36 Elmer de Looff
</code></pre>
371 36 Elmer de Looff
372 36 Elmer de Looff
<pre><code class="python">
373 36 Elmer de Looff
>>> parser.Parse('message.utp', message='"Hello"')
374 36 Elmer de Looff
</code></pre>
375 36 Elmer de Looff
376 36 Elmer de Looff
<pre><code class="html">
377 36 Elmer de Looff
And he said: &quot;Hello&quot;
378 36 Elmer de Looff
</code></pre>
379 36 Elmer de Looff
380 36 Elmer de Looff
Only when you use another tag function, or specifically tell @TemplateParser@ to push the _raw_ tag value into the output, are the quotes allowed through unchanged:
381 36 Elmer de Looff
382 36 Elmer de Looff
<pre><code class="html">
383 36 Elmer de Looff
And he said: [message|raw]
384 36 Elmer de Looff
</code></pre>
385 36 Elmer de Looff
386 36 Elmer de Looff
<pre><code class="python">
387 36 Elmer de Looff
>>> parser.Parse('message.utp', message='"Hello"')
388 36 Elmer de Looff
</code></pre>
389 36 Elmer de Looff
390 36 Elmer de Looff
<pre><code class="html">
391 36 Elmer de Looff
And he said: "Hello"
392 36 Elmer de Looff
</code></pre>
393 36 Elmer de Looff
394 35 Elmer de Looff
h3. Predefined tag functions
395 1 Elmer de Looff
396 36 Elmer de Looff
* *html* &ndash; This tag function escapes content to be safe for inclusion in HTML pages. This means that the ampersand ( & ), single and double quotes ( '  &nbsp;and&nbsp; " ) and the pointy brackets ( < &nbsp;and&nbsp; > ) are converted to their respective "character entity references":http://en.wikipedia.org/wiki/Character_entity_reference
397 1 Elmer de Looff
* _default_ &ndash; This is the tag function that will be executed when no other tag functions have been specified for a tag. By default, this will do the same as the *html* tag function. This can be adjusted by assigning another tag function to this name.
398 35 Elmer de Looff
* *raw* &ndash; This tag function passes the tag through without change. This is the function to use when you have no tag function to apply, but do not want the tag to be HTML-escaped.
399 36 Elmer de Looff
* *url* &ndash; This tag function prepares the tag for use in URLs. Space are converted to plus-signs ( + ), and other characters that are considered unsafe for URLs are converted to "percent-notation":http://en.wikipedia.org/wiki/Percent-encoding.
400 1 Elmer de Looff
401 35 Elmer de Looff
h3. Adding custom functions
402 35 Elmer de Looff
403 35 Elmer de Looff
Custom methods can be added to a @Parser@ object using the method @RegisterFunction@. This takes a name, and a single-argument function. When this function is encountered in a tag, it will be given the current tag value, and its result will be output to the template, or passed into the next function:
404 35 Elmer de Looff
405 35 Elmer de Looff
<pre><code class="python">
406 35 Elmer de Looff
>>> from uweb import templateparser
407 35 Elmer de Looff
>>> parser = templateparser.Parser()
408 35 Elmer de Looff
>>> parser.RegisterFunction('len', len)
409 35 Elmer de Looff
>>> template = 'The number of people in this group: [people|len].'
410 35 Elmer de Looff
>>> parser.ParseString(template, elements=['Eric', 'Michael', 'John', 'Terry'])
411 35 Elmer de Looff
'The number of people in this group: 4.'
412 35 Elmer de Looff
</code></pre>
413 35 Elmer de Looff
414 40 Elmer de Looff
*N.B.:* Using custom functions (or in fact any function other than _html_ or no function) will suppress HTML escaping. If your content is still user-driven, or not otherwise made safe for output, *it is strongly recommended you apply html escaping*. This can be achieved by chaining functions, as explained below.
415 40 Elmer de Looff
416 41 Elmer de Looff
h3. Function chaining
417 35 Elmer de Looff
418 35 Elmer de Looff
Multiple function calls can be chained after one another. The functions are processed left to right, and the result of each function is passed into the next, without any intermediate editing or changes:
419 35 Elmer de Looff
420 35 Elmer de Looff
Setting up the parser and registering our tag function
421 35 Elmer de Looff
<pre><code class="python">
422 35 Elmer de Looff
>>> from uweb import templateparser
423 35 Elmer de Looff
>>> parser = templateparser.Parser()
424 35 Elmer de Looff
>>> parser.RegisterFunction('first', lambda x: x[0])
425 35 Elmer de Looff
</code></pre>
426 35 Elmer de Looff
427 35 Elmer de Looff
Working just one tag function returns the first element from the list:
428 35 Elmer de Looff
<pre><code class="python">
429 35 Elmer de Looff
>>> template = 'The first element of list: [elements|first].'
430 35 Elmer de Looff
>>> parser.ParseString(template, elements=['Eric', 'Michael', 'John', 'Terry'])
431 35 Elmer de Looff
'The first element of list: Eric.'
432 35 Elmer de Looff
</code></pre>
433 35 Elmer de Looff
434 35 Elmer de Looff
Repeating the function on the string returns the first character from that string:
435 35 Elmer de Looff
<pre><code class="python">
436 35 Elmer de Looff
>>> template = 'The first element of the first element of list: [elements|first|first].'
437 35 Elmer de Looff
>>> parser.ParseString(template, elements=['Eric', 'Michael', 'John', 'Terry'])
438 35 Elmer de Looff
'The first element of the first element of list: E.'
439 35 Elmer de Looff
</code></pre>
440 11 Elmer de Looff
441 44 Elmer de Looff
h3. Valid function name characters
442 44 Elmer de Looff
443 44 Elmer de Looff
Tag function names may be constructed from upper and lower case letters, numbers, underscores and dashes. There are no restrictions on first character, only a minimum length of one. Regex-wise, they need to match @[\w-]+@
444 44 Elmer de Looff
445 23 Elmer de Looff
h2. TemplateLoop
446 11 Elmer de Looff
447 23 Elmer de Looff
As a language construct, TemplateParser has an understanding of iteration. The @TemplateLoop@ can be compared to the Python @for@-loop, or the @foreach@ construct in other languages (lazy iteration over the values of an iterable).
448 1 Elmer de Looff
449 23 Elmer de Looff
h3. Syntax and properties
450 23 Elmer de Looff
451 23 Elmer de Looff
*Syntax: @{{ for local_var in [collection] }}@*
452 20 Elmer de Looff
* The double accolades (curly braces) indicate the beginning and end of the construct;
453 20 Elmer de Looff
* The @for@ keyword indicates the structure to execute;
454 20 Elmer de Looff
* @local_var@ is the name which references the loop variable;
455 20 Elmer de Looff
* @[collection]@ is the tag that provides the iteratable.
456 20 Elmer de Looff
457 20 Elmer de Looff
*Properties*
458 20 Elmer de Looff
* The local name is stated without brackets (as it's no tag itself)
459 1 Elmer de Looff
* When it needs to be placed in the output, the local name should have brackets (like any other tag)
460 20 Elmer de Looff
* *N.B.* The local variable does _not_ bleed into the outer scope after the loop has completed.
461 20 Elmer de Looff
 It is therefore possible (though not recommended) to name the loop variable after the iterable: @{{ for collection in [collection] }}@.
462 20 Elmer de Looff
463 23 Elmer de Looff
h3. Example of a @TemplateLoop@
464 20 Elmer de Looff
465 20 Elmer de Looff
<pre><code class="html">
466 20 Elmer de Looff
<html>
467 20 Elmer de Looff
  <body>
468 20 Elmer de Looff
    <ul>
469 20 Elmer de Looff
    {{ for name in [presidents] }}
470 20 Elmer de Looff
      <li>President [name]</li>
471 20 Elmer de Looff
    {{ endfor }}
472 20 Elmer de Looff
    </ul>
473 20 Elmer de Looff
  </body>
474 20 Elmer de Looff
</html>
475 20 Elmer de Looff
</code></pre>
476 20 Elmer de Looff
477 20 Elmer de Looff
<pre><code class="python">
478 20 Elmer de Looff
>>> parser.Parse('rushmore.utp', presidents=['Washington', 'Jefferson', 'Roosevelt', 'Lincoln'])
479 20 Elmer de Looff
</code></pre>
480 20 Elmer de Looff
481 20 Elmer de Looff
<pre><code class="html">
482 20 Elmer de Looff
<html>
483 20 Elmer de Looff
  <body>
484 1 Elmer de Looff
    <ul>
485 1 Elmer de Looff
      <li>President Washington</li>
486 1 Elmer de Looff
      <li>President Jefferson</li>
487 1 Elmer de Looff
      <li>President Roosevelt</li>
488 1 Elmer de Looff
      <li>President Lincoln</li>
489 1 Elmer de Looff
    </ul>
490 1 Elmer de Looff
  </body>
491 1 Elmer de Looff
</html>
492 1 Elmer de Looff
</code></pre>
493 1 Elmer de Looff
494 1 Elmer de Looff
h2. Inlining templates
495 21 Elmer de Looff
496 21 Elmer de Looff
Often, there will be snippets of a template that will see a lot of reuse. Page headers and footers are often the same on many pages, and having several redundant copies means that changes will have to be replicated to each of these occurrances. To reduce the need for this, TemplateParser has an @inline@ statement. Using this you can specify a template that is available in the @[[TemplateParser#Parser]]@ instance and the statement will be replaced by the template.
497 21 Elmer de Looff
498 21 Elmer de Looff
Of course, if the inlined template is not already in the @Parser@ instance, the autoloading mechanism will trigger, and the named template will be search for in the @Parser@'s template directory.
499 21 Elmer de Looff
500 21 Elmer de Looff
First, we will define our inline template, @'inline_hello.utp'@:
501 21 Elmer de Looff
502 21 Elmer de Looff
<pre><code class="html">
503 21 Elmer de Looff
<p>Hello [name]</p>
504 21 Elmer de Looff
</code></pre>
505 21 Elmer de Looff
506 21 Elmer de Looff
Secondly, our main template, @'hello.utp'@:
507 21 Elmer de Looff
508 21 Elmer de Looff
<pre><code class="html">
509 21 Elmer de Looff
<h1>Greetings</h1>
510 21 Elmer de Looff
{{ inline inline_hello.utp }}
511 21 Elmer de Looff
</code></pre>
512 21 Elmer de Looff
513 21 Elmer de Looff
Then we parse the template:
514 21 Elmer de Looff
515 21 Elmer de Looff
<pre><code class="python">
516 21 Elmer de Looff
>>> parser.Parse('hello.utp', name='Dr John')
517 21 Elmer de Looff
</code></pre>
518 21 Elmer de Looff
519 21 Elmer de Looff
<pre><code class="html">
520 21 Elmer de Looff
<h1>Greetings</h1>
521 21 Elmer de Looff
<p>Hello Dr John</p>
522 21 Elmer de Looff
</code></pre>
523 11 Elmer de Looff
524 11 Elmer de Looff
h2. Conditional statements
525 11 Elmer de Looff
526 22 Elmer de Looff
Often, you'll want the output of your template to be dependent on the value, presence, or boolean value of another tag. For instance, we may want a print a list of attendees to a party. We start the @if@ conditional by checking the boolean value of the @attendees@ tag. If this list if not-empty, we will print the attendee names, but if it's empty (or contains only a single entry), we'll tell the user in more intelligent ways than giving them a list with zero entries:
527 22 Elmer de Looff
528 22 Elmer de Looff
<pre><code class="html">
529 22 Elmer de Looff
<h1>Party attendees</h1>
530 22 Elmer de Looff
{{ if len([attendees]) > 1 }}
531 22 Elmer de Looff
  <ol>
532 22 Elmer de Looff
    {{ for attendee in [attendees] }}
533 22 Elmer de Looff
    <li>[attendee:name]</li>
534 22 Elmer de Looff
    {{ endfor }}
535 22 Elmer de Looff
  </ol>
536 22 Elmer de Looff
{{ elif [attendees] }}
537 22 Elmer de Looff
  <p>only [attendees:0:name] is attending.</p>
538 22 Elmer de Looff
{{ else }}
539 22 Elmer de Looff
  <p>There are no registered attendees yet.</p>
540 22 Elmer de Looff
{{ endif }}
541 22 Elmer de Looff
</code></pre>
542 22 Elmer de Looff
543 22 Elmer de Looff
For the case where there are several attendees:
544 22 Elmer de Looff
545 22 Elmer de Looff
<pre><code class="python">
546 22 Elmer de Looff
>>> parser.Parse('party.utp', attendees=[
547 22 Elmer de Looff
...    {'name': 'Livingstone'},
548 22 Elmer de Looff
...    {'name': 'Cook'},
549 22 Elmer de Looff
...    {'name': 'Drake'}])
550 22 Elmer de Looff
</code></pre>
551 22 Elmer de Looff
552 22 Elmer de Looff
<pre><code class="html">
553 22 Elmer de Looff
<h1>Party attendees</h1>
554 22 Elmer de Looff
<ol>
555 22 Elmer de Looff
  <li>Livingstone</li>
556 22 Elmer de Looff
  <li>Cook</li>
557 22 Elmer de Looff
  <li>Drake</li>
558 22 Elmer de Looff
</ol>
559 22 Elmer de Looff
</code></pre>
560 22 Elmer de Looff
561 22 Elmer de Looff
For the case where there is one attendee:
562 22 Elmer de Looff
563 22 Elmer de Looff
<pre><code class="python">
564 22 Elmer de Looff
>>> parser.Parse('party.utp', attendees=[{'name': 'Johnny'}])
565 22 Elmer de Looff
</code></pre>
566 22 Elmer de Looff
567 22 Elmer de Looff
<pre><code class="html">
568 22 Elmer de Looff
<h1>Party attendees</h1>
569 22 Elmer de Looff
<p>Only Johnny is attending.</p>
570 22 Elmer de Looff
</code></pre>
571 22 Elmer de Looff
572 22 Elmer de Looff
And in the case where there are no attendees:
573 22 Elmer de Looff
574 22 Elmer de Looff
<pre><code class="python">
575 22 Elmer de Looff
>>> parser.Parse('party.utp', attendees=[])
576 22 Elmer de Looff
</code></pre>
577 22 Elmer de Looff
578 22 Elmer de Looff
<pre><code class="html">
579 22 Elmer de Looff
<h1>Party attendees</h1>
580 22 Elmer de Looff
<p>There are no registered attendees yet.</p>
581 22 Elmer de Looff
</code></pre>
582 22 Elmer de Looff
583 22 Elmer de Looff
h3. Properties of conditional statements
584 22 Elmer de Looff
585 22 Elmer de Looff
* *All template keys must be referenced as proper tag*
586 22 Elmer de Looff
 This is to prevent mixing of the template variables with the functions and reserved names of Python itself. Conditional expressions are evaluated using @eval()@, and proper tags are replaced by temporary names, the values of which are stored in a retrieve-on-demand dictionary. This makes them perfectly safe with regard to the value of template replacements, but some care should be taken with the writing of the conditional expressions.
587 22 Elmer de Looff
* *It is possible to index tags in conditional statements*
588 22 Elmer de Looff
 This allows for decisions based on the values in those indexes/keys. For instance, @Person@ objects can be checked for gender, so that the correct gender-based icon can be displayed next to them.
589 22 Elmer de Looff
* *Referencing a tag or index that doesn't exist raises @TemplateNameError*
590 22 Elmer de Looff
 Unlike in regular template text, there is no suitable fallback value for a tag or index that cannot be retrieved. However, in most cases this can be prevented by making use of the following property:
591 22 Elmer de Looff
* *Statement evaluation is lazy*
592 22 Elmer de Looff
 Template conditions are processed left to right, and short-circuited where possible. If the first member of an @or@ group succeeds, the return value is already known. Similarly, if the first member of an @and@ group fails, the second part need not be evaluated. This way @TemplateNameErrors@ can often be prevented, as in most cases, presence of indexes can be confirmed before accessing.
593 22 Elmer de Looff
594 22 Elmer de Looff
595 11 Elmer de Looff
h2. Template unicode handling
596 11 Elmer de Looff
597 11 Elmer de Looff
Any @unicode@ object found while parsing, will automatically be encoded to UTF-8:
598 11 Elmer de Looff
599 11 Elmer de Looff
<pre><code class="python">
600 11 Elmer de Looff
>>> template = 'Underdark [love] [app]'
601 11 Elmer de Looff
>>> output = parser.ParseString(template, love=u'\u2665', app=u'\N{micro sign}Web')
602 11 Elmer de Looff
>>> output
603 12 Elmer de Looff
'Underdark \xe2\x99\xa5 \xc2\xb5Web'  # The output in its raw UTF-8 representation
604 11 Elmer de Looff
>>> output.decode('UTF8')
605 12 Elmer de Looff
u'Underdark \u2665 \xb5Web'           # The output converted to a Unicode object
606 19 Elmer de Looff
>>> print output
607 19 Elmer de Looff
Underdark ♥ µWeb                      # And the printed UTF-8 as we desired it.
608 14 Elmer de Looff
</code></pre>