Subsections


1. First Steps


1.1 Simple Example

We will start with a very simple example. First we need a template file which we are going to use. For now it will be a simple html file which I store as templates/beispiel.tpl

beispiel.tpl

<html>
    <head>
        <title>{TITLE}</title>
    </head>
    <body>
        <h1>{HEADLINE}</h1>
        <p>{PARAGRAPH}</p>
    </body>
</html>

As you can see there are three words placed between curly brackets. These parts are placeholders for template variables. In the final output they will be replaced with content we specify.

The next step is creating a simple python file which uses pyTemple to compile and execute the above example. I store it as beispiel.py.

beispiel.py

from tplEngine import *
template = TplTemplate('beispiel', 'beispiel.tpl')

template.assign_vars({
    'TITLE': 'Example Page',
    'HEADLINE': 'Simple Template Example',
    'PARAGRAPH': "Aren't all these possibilities great? You can do really many things, read on."})
    
print template.get_executed()

Now we need to create a directory template_cache and we can go to the console and try our script.

Console

$ python beispiel.py
<html>
    <head>
        <title>Example Page</title>
    </head>
    <body>
        <h1>Simple Template Example</h1>
        <p>Aren't all these possibilities great? You can do so many things, read on.</p>
    </body>
</html>

As you can see {TITLE}, {HEADLINE} and {PARAGRAPH} have been replaced with the contents we specified in beispiel.py. It really is that simple.


1.2 Using Functions And Operators

Of course you can do more than just inserting data. You may use functions and operators to change the output. Since the template language is object oriented you can nearly all functions are methods but they can be used as simple functions as well. From now on I will only use the parts of a file needed to understand what I am explaining.

beispiel.tpl

Operators:
TEXT + " mit Anhang":                   {TEXT + " mit Anhang"}
TEXT + " Text" * 10:                    {TEXT + " Text" * 10}
ZAHL + 3:                               {ZAHL + 3}
(ZAHL * 3) + 5 - (1/(7*ZAHL*ZAHL)):     {(ZAHL * 3) + 5 - (1/(7*ZAHL*ZAHL))}
BOOLEAN:                                {BOOLEAN}
(BOOLEAN and BOOLEAN2) or BOOLEAN3:     {(BOOLEAN and BOOLEAN2) or BOOLEAN3}

Functions:
{len(TEXT)} is the same as {TEXT.len()}.
{upper(TEXT)} is the same as {TEXT.upper()}.
TEXT contains {TEXT.count("e")} 'e's.
The first e is at position {find(TEXT, "e")}.

Be aware, you always have to use double quotes in template commands!

And we need a python file assigning values.

beispiel.py

from tplEngine import *
template = TplTemplate('beispiel', 'beispiel.tpl')

template.assign_vars({
    'TEXT': 'Dieser Text',
    'ZAHL': 5,
    'BOOLEAN': False,
    'BOOLEAN2': True,
    'BOOLEAN3': True})
    
print template.get_executed()

So let's see what it looks like.

Console

$ python beispiel.py
TEXT + " mit Anhang":                   Dieser Text mit Anhang
TEXT + " Text" * 10:                    Dieser Text Text Text Text Text Text Text Text Text Text Text
ZAHL + 3:                               8
(ZAHL * 3) + 5 - (1/(7*ZAHL*ZAHL)):     20
BOOLEAN:                                False
(BOOLEAN and BOOLEAN2) or BOOLEAN3:     True

Functions:
11 is the same as 11.
DIESER TEXT is the same as DIESER TEXT.
TEXT contains 3 "e"s.
The first e is at position 2.

See About this document... for information on suggesting changes.