SourceForge.net Logo Support This Project

PYthon Template v0.2.1


After looking for a pythonish alternative to JSP, ASP and PHP and finding several abandond, half finished overly complex alternatives I decided to write an extremely simple templating tool that is python through and through. PYT is a 250 (ish) line pure python script that provides all the tools necessary to do HTML, XML, or, in fact, any kind of templating.

Download


The latest package is available on sourceforge: pyt-0.2.1
Previous packages are also available: pyt
You can browse source here.

License

PYT v0.2.2 is released under the LGPL. You can use it in your software and process templates without having to release your software or templates under any particular license. If you distribute PYT with or without modification you must make source code available and release it under a compatible license as per the LGPL. A copy of the LGPL is available at http://www.gnu.org/copyleft/lesser.html

Changes Since v0.2


At the request of an early adopter I have added support for easier integration with mod_python. There are two new features to support mod_python:
  1. When calling module run() with a name that is not __main__, pyt will execute the latest template and return the text string (the pyt class always returns the text string).
  2. You can setup pyt as an output filter to automatically run all .pyt files (or any extension you want to use). See httpd.conf and mime.types for the modifications you need to make to your apache configuration.

Changes Since v0.1


PYT v0.2 changes the interface from a static function to a class. This allows you to precompile and save a template into a ptc file. This is a similar mechanism to the python byte compiler though PYT uses pickle (or cPickle where available) to save the internal data structure.
<[embed arg]> is now supported. arg is the name of a file or path to a file relative to the current working directory as returned by os.getcwd().

License


PYT v0.2 is release under the LGPL. You can use it in your software and process templates without having to release your software or templates under any particular license. If you distribute PYT with or without modification you must make source code available and release it under a compatible license as per the LGPL. A copy of the LGPL is available at http://www.gnu.org/copyleft/lesser.html

Getting Started


There are several samples to get you started. Each performs an identical function but via various methods. The first is a pair of files: sample.py and sample.html. sample.py controls the program and loads the template file sample.html. The second example has the pyt extension and is structured as a more typical cgi script with the html file embeded. The third is a precompiled versin of sample.pyt. There is a brief example showing how to compile, save and load a PYT script in compile.py. The samples can be invoked as follows:
python sample.py

	or

python pyt.py sample.pyt

	or

python pyt.py sample.ptc

	or

python compile.py

PYT language


PYT is designed to be as simple and pythonic as possible. PYT is build around dictionary string substituion, a built in feature in python. Any variable in the pyt namespace (by default the global namespace) can be printed by entering %(var)f where var is the name of the variable and f is a standard printf formatter. PYT extends this to support nested variables a.b.c and and get items a[b] also literal get items a['b'] or a[0] or any combination.

In addition to variable substitution, pyt supports six commands using a syntax such as <[cmd args]>content<[close]>. The commands are as follows:

<[link url]>text<[link]>

Will insert <a href="url">text</a>. Text can be any literal text or nested commands or variables using variable substituion. url can be any valid python code including variables, methods or string literals. Future releases will extend the functionality of the link command.

<[embed arg]>

embed currently supports server side embeding. arg is the path to a server side file relative to the current working directory as returned from os.getcwd(). Emebded files are not precompile. In the future embed will support more advanced embedding including precompiled (and precompiling) templates on the server and on the client side (using the <object> tag).

<[exec statements]>

Executes statements using python exec. This can be used to create or change variables or invoke methods. Statements can be ; delimited or multiline. The result of any statement is not displayed.

<[list]>text<[for var in set if stmt]>

Performs a list comprehension introducing elements from set using variable var, one at a time. if stmt is an optional conditional that will cause the comprehension to skip the current variable if false. As with python, the last variable remains in scope when the list comprehension finishes. If the list is empty, the variable will not be present after the list comprehension.

<[if cond]>text<[else cond]>text<[else]>text<[end]>

Standard conditonal evaluation.

<[while cond]>text<[loop]>

Simple conditional loop for rare cases when list comprehension cannot be used. The danger with conditional loops is the possibility of infinite loops which can bring your server (or at least your process quota) to a crawl.

Programming Caveats


PYT provides no local scoping besides the global scope. This may change in the future if it makes sense to do so.