Project

General

Profile

TemplateParser » History » Version 4

Version 3 (Elmer de Looff, 2012-02-09 15:38) → Version 4/56 (Elmer de Looff, 2012-02-09 16:34)

h1. TemplateParser

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:
* The [[TemplateParser#template|Template class]], used to parse the templating language
* The [[TemplateParser#parser|Parser class]], which provides template loading and caching
* [[TemplateParser#using|Using TemplateParser]] inside a µWeb PageMaker
* A detailed explanation of the [[TemplateParser#syntax|templating language syntax]], constructs and behaviors

First though, to help with understanding the TemplateParser, a minimal size template document:

<pre><code class="html">
Hello [title] [name]
</code></pre>


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.



h1(template). Template class

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.

h2. Creating a template

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]]:

<pre><code class="python">
import templateparser
>>> template = templateparser.Template('Hello [title] [name]')
>>> template
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
</code></pre>

Above can be seen the various parts of the template, which will be combined to output once parsed.

h2. Loading a template from file

The @Template@ class provides a @classmethod@ called @FromFile@, which loads the template at the path.

Loading a template named @example.utp@ from the current working directory:

<pre><code class="python">
import templateparser
>>> template = templateparser.Template.FromFile('example.utp')
>>> template
Template([TemplateText('Hello '), TemplateTag('[title]'), TemplateText(' '), TemplateTag('[name]')])
</code></pre>

h3. Parsing a template

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.

<pre><code class="python">
import templateparser
>>> template = templateparser.Template('Hello [title] [name]')
>>> template.Parse(title='senor')
'Hello senor [name]'
</code></pre>


h1(parser). Parser class

h1(using). Using TemplateParser inside µWeb

h1(syntax). Templating language syntax