Python Templating Engine

Download
 PTE is my Python Templating Engine. It allows one to embed python within HTML much like PHP or JSP. However PTE has some different design goals.

  1. I hate writing PHP. Don't even get me started on Perl. I love writing
     Python.

  2. Python is much cleaner and more compact than Java.

  3. PTE is very extensible. Writing new high-level tags is very simple and can
     simply and abstract mundane tasks such as database access, sending mail,
     user cookies, etc...

  4. PTE has a very light-weight install. Unlike PHP or JSP, installing PTE
     means downloading a handful of files and you're set. Administrative
     privileges are not required.

 Java's JSP is a very good analogy for what PTE is to python. PTE borrows many ideas from JSP, such as dynamic code generation and compilation. While JSP is a great technology, installing a Java application server such as Weblogic or Tomcat is an administrative pain, and overkill for smaller projects. PTE is intended to bring an easy, robust templating system for smaller projects.

 The current alternative to embedded languages like PHP, is to use standard Python or Perl CGI. Anyone who's had to write a CGI script with copius print statement interleaved with script logic knows this is messy and hard to maintain.

 PTE offers the best of these common solutions. The programming ease of an embedded language, the robustness of python, and the simplicity of CGI.

Some Code Examples

Here's a sample script that just shows off syntax and functionality. Pretty self-explanitory what everything does.


#!/usr/bin/pte_launcher
Content-Type: text/html


<?inline header.html> <!-- good ol' file inclusion -->
<?python>
class Clown: #some literal python
  def peoplecanthear(self):
    self.frobnicate()

foo = 42
<?endpython>

My favorite number is ${foo}

Some variables come pre-defined... all CGI parameters are in a hash called CGI.
<?for key,value in CGI.items()>
  ${key} = ${value}
<?endfor>

You can use literal python expressions within the curly braces.
${ "hello world".upper() }

Tag libraries allow you to define your own tags. Here are a few I use...

<?db driver="MySQLdb" server=localhost db="mysql" user='root'>
<?sql query="select * from user" var=results>

<table>
<?for row in results>
  <tr>
  <?for field in row>
    <td>${field}</td>
  <?endfor>
  </tr>
<?endfor>
</table>

Now let's send some mail...

<?mail to='someone@somewhere.com'
       from='me@here.com'
       server='localhost:25'>
Subject: This is email number ${foo}


Hey, I thought you'd like to see a copy of my passwd file... here it is.

<?inline /etc/passwd>

Bye!
<?endmail>

You can also include other pte scripts like so...
<?include someTemplate.tmpl>
Note the code is generated only once, if someTemplate.tmpl is changed later
on then you must update the timestamp of this script for it to be
recompiled. However, if you really don't care about performance and want
to dynamically include a template at run time (ie. parse it every time
your page is loaded) you can use <?runtemplate someTemplate.tmpl>

Hm...what else? Oh, here's my home dir: <?run ls /home/cdavis/>
That's all I can think of for now...

Pretty easy? The code that defines the mail tags is like 25 lines... the code to define the database tags is like 35 lines. Pretty damn simple too. Enjoy.

So what's the downside?

 So PTE has several downfalls, most of which I don't care about too much.

 1. It's probably slower than PHP/JSP. This is just a guess, I haven't done
    any benchmarks. After the first run, it should be *slightly* faster
    than straight up python cgi.

 2. The tag programming interface has some awkward caveats at the moment, I
    really hope to resolve these.

 3. It's harder to secure on a multi-user webserver. PTE compiles your .tmpl
    file into an object file in the same directory, and thus requires write
    access for the webserver user there. This shouldn't be too hard to fix.

 4. The installer is quite bad.

I want more documentation! I want new feature X!

Drop me a line, chrismd at gmail.com