Project

General

Profile

TemplateParser » History » Version 5

Elmer de Looff, 2012-02-09 16:36

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 4 Elmer de Looff
18 5 Elmer de Looff
h1(#template). Template class
19 4 Elmer de Looff
20 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.
21 4 Elmer de Looff
22 4 Elmer de Looff
h2. Creating a template
23 4 Elmer de Looff
24 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]]:
25 4 Elmer de Looff
26 4 Elmer de Looff
<pre><code class="python">
27 4 Elmer de Looff
import templateparser
28 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
29 4 Elmer de Looff
>>> template
30 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
31 4 Elmer de Looff
</code></pre>
32 4 Elmer de Looff
33 4 Elmer de Looff
Above can be seen the various parts of the template, which will be combined to output once parsed.
34 4 Elmer de Looff
35 4 Elmer de Looff
h2. Loading a template from file
36 4 Elmer de Looff
37 4 Elmer de Looff
The @Template@ class provides a @classmethod@ called @FromFile@, which loads the template at the path.
38 4 Elmer de Looff
39 4 Elmer de Looff
Loading a template named @example.utp@ from the current working directory:
40 4 Elmer de Looff
41 4 Elmer de Looff
<pre><code class="python">
42 4 Elmer de Looff
import templateparser
43 4 Elmer de Looff
>>> template = templateparser.Template.FromFile('example.utp')
44 4 Elmer de Looff
>>> template
45 4 Elmer de Looff
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
46 4 Elmer de Looff
</code></pre>
47 4 Elmer de Looff
48 5 Elmer de Looff
h2. Parsing a template
49 4 Elmer de Looff
50 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.
51 4 Elmer de Looff
52 4 Elmer de Looff
<pre><code class="python">
53 4 Elmer de Looff
import templateparser
54 4 Elmer de Looff
>>> template = templateparser.Template('Hello [title] [name]')
55 4 Elmer de Looff
>>> template.Parse(title='senor')
56 4 Elmer de Looff
'Hello senor [name]'
57 4 Elmer de Looff
</code></pre>
58 1 Elmer de Looff
59 5 Elmer de Looff
h1(#parser). Parser class
60 1 Elmer de Looff
61 5 Elmer de Looff
h1(#using). Using TemplateParser inside µWeb
62 1 Elmer de Looff
63 5 Elmer de Looff
h1(#syntax). Templating language syntax