Project

General

Profile

TemplateParser » History » Version 7

Elmer de Looff, 2012-02-09 17:18

1 1 Elmer de Looff
h1. TemplateParser
2 1 Elmer de Looff
3 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:
4 3 Elmer de Looff
* The [[TemplateParser#template|Template class]], used to parse the templating language
5 3 Elmer de Looff
* The [[TemplateParser#parser|Parser class]], which provides template loading and caching
6 3 Elmer de Looff
* [[TemplateParser#using|Using TemplateParser]] inside a µWeb PageMaker
7 3 Elmer de Looff
* A detailed explanation of the [[TemplateParser#syntax|templating language syntax]], constructs and behaviors
8 1 Elmer de Looff
9 1 Elmer de Looff
First though, to help with understanding the TemplateParser, a minimal size template document:
10 1 Elmer de Looff
11 1 Elmer de Looff
<pre><code class="html">
12 1 Elmer de Looff
Hello [title] [name]
13 1 Elmer de Looff
</code></pre>
14 4 Elmer de Looff
15 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.
16 1 Elmer de Looff
17 5 Elmer de Looff
h1(#template). Template class
18 4 Elmer de Looff
19 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.
20 4 Elmer de Looff
21 4 Elmer de Looff
h2. Creating a template
22 4 Elmer de Looff
23 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]]:
24 4 Elmer de Looff
25 4 Elmer de Looff
<pre><code class="python">
26 4 Elmer de Looff
import templateparser
27 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
28 4 Elmer de Looff
>>> template
29 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
30 4 Elmer de Looff
</code></pre>
31 4 Elmer de Looff
32 4 Elmer de Looff
Above can be seen the various parts of the template, which will be combined to output once parsed.
33 4 Elmer de Looff
34 4 Elmer de Looff
h2. Loading a template from file
35 4 Elmer de Looff
36 4 Elmer de Looff
The @Template@ class provides a @classmethod@ called @FromFile@, which loads the template at the path.
37 4 Elmer de Looff
38 4 Elmer de Looff
Loading a template named @example.utp@ from the current working directory:
39 4 Elmer de Looff
40 4 Elmer de Looff
<pre><code class="python">
41 4 Elmer de Looff
import templateparser
42 4 Elmer de Looff
>>> template = templateparser.Template.FromFile('example.utp')
43 4 Elmer de Looff
>>> template
44 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
45 4 Elmer de Looff
</code></pre>
46 4 Elmer de Looff
47 5 Elmer de Looff
h2. Parsing a template
48 4 Elmer de Looff
49 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.
50 4 Elmer de Looff
51 4 Elmer de Looff
<pre><code class="python">
52 4 Elmer de Looff
import templateparser
53 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
54 4 Elmer de Looff
>>> template.Parse(title='senor')
55 4 Elmer de Looff
'Hello senor [name]'
56 4 Elmer de Looff
</code></pre>
57 1 Elmer de Looff
58 1 Elmer de Looff
h1(#parser). Parser class
59 6 Elmer de Looff
60 6 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.
61 6 Elmer de Looff
62 6 Elmer de Looff
Creating a parser object, and loading the 'example.utp' template from the 'templates' directory works like this:
63 6 Elmer de Looff
64 6 Elmer de Looff
<pre><code class="python">
65 6 Elmer de Looff
import templateparser
66 7 Elmer de Looff
>>> # This sets the 'templates' directory as the search path for AddTemplate
67 7 Elmer de Looff
>>> parser = templateparser.Parser('templates')
68 7 Elmer de Looff
>>> # Loads the 'templates/example.utp' and stores it as 'example.utp':
69 7 Elmer de Looff
>>> template = parser.AddTemplate('example.utp')
70 6 Elmer de Looff
>>> template.Parse(title='mister', name='Bob Dobalina')
71 1 Elmer de Looff
'Hello mister Bob Dobalina'
72 6 Elmer de Looff
</code></pre>
73 1 Elmer de Looff
74 7 Elmer de Looff
The @AddTemplate@ method takes a second optional argument, which allows us to give the template a different name in the cache. However, we have not covered using this cache yet. The @Parser@ object behaves like a slightly modified dictionary to achieve this. Retrieving keys yields the associated template. Keys that are not present in the cache are _automatically_ retrieved from the filesystem:
75 1 Elmer de Looff
76 1 Elmer de Looff
<pre><code class="python">
77 1 Elmer de Looff
import templateparser
78 7 Elmer de Looff
>>> parser = templateparser.Parser('templates')
79 7 Elmer de Looff
>>> parser
80 7 Elmer de Looff
Parser({})  # The parser is empty (has no cached templates)
81 7 Elmer de Looff
>>> # Automatically loads the named template from the 'templates' directory:
82 7 Elmer de Looff
>>> parser['example.utp'].Parse(title='mister', name='Bob Dobalina')
83 1 Elmer de Looff
'Hello mister Bob Dobalina'
84 7 Elmer de Looff
>>> parser
85 7 Elmer de Looff
Parser({'example.utp': Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])})
86 7 Elmer de Looff
</code></pre>
87 7 Elmer de Looff
88 7 Elmer de Looff
If these cannot be found, @TemplateReadError@ is raised:
89 7 Elmer de Looff
90 7 Elmer de Looff
<pre><code class="python">
91 7 Elmer de Looff
import templateparser
92 7 Elmer de Looff
>>> parser = templateparser.Parser('templates')
93 6 Elmer de Looff
>>> parser['bad_template.utp'].Parse(failure='imminent')
94 6 Elmer de Looff
Traceback (most recent call last):
95 6 Elmer de Looff
  File "<stdin>", line 1, in <module>
96 7 Elmer de Looff
  File "/var/lib/underdark/libs/uweb/templateparser.py", line 147, in __getitem__
97 6 Elmer de Looff
    self.AddTemplate(template)
98 7 Elmer de Looff
  File "/var/lib/underdark/libs/uweb/templateparser.py", line 171, in AddTemplate
99 6 Elmer de Looff
    raise TemplateReadError('Could not load template %r' % template_path)
100 6 Elmer de Looff
underdark.libs.uweb.templateparser.TemplateReadError: Could not load template 'templates/bad_template.utp'
101 6 Elmer de Looff
</code></pre>
102 6 Elmer de Looff
103 1 Elmer de Looff
104 5 Elmer de Looff
h1(#using). Using TemplateParser inside µWeb
105 1 Elmer de Looff
106 5 Elmer de Looff
h1(#syntax). Templating language syntax