|
Blog -
Program
|
|
Written by Dennis Reinhardt
|
|
Sunday, 25 January 2009 12:12 |
|
I have defined a basic templating system for displaying html in TextToolKit. An entry starting with a "$" is expanded out into text via a subroutine call, file lookup, or static text. Thus, I can define a template file named hello.tpl consisting of: $std_head {h1}Hello{/h1} $std_foot
and the "$" lines will be expanded out, perhaps via repeated substitution. In the python file called by the url http://localhost:32155/hello.py, the line:
html = tpl.expand("$hello")
expands all this out into html code.
In theory, the template fille hello.tpl contains the presentation and the python file hello.py contains the logic, a clean separation. But ...
In theory there is no difference between theory and practice. In practice there is.
-- Yogi Berra
Consider the first working screen I am developing: the file open screen mockup shown. All of the elements have a clickable URL and source definition. A path such as a/b/c/ has three clickable elements and three definition. If we tried to specify this in a template, we might be tempted to write something like
$elem1/$elem2/$elem3
and let the expansion of each element expand to something like (simplified):
{a href=...}...{/a}
but the number of elements in the current path is dynamic. Some templating systems allow for this by putting iteration control in the templating language. This seems like the wrong approach to me. If iteration control is needed, that should be coded in Python.
Perhaps an intermediate approach can be used. Rather than define each element of the path with a template variable, a $path variable itself can be be defined and expanded out in Python code to: {ul} {li}{a href=...}...{/a}/{/li} {li}{a href=...}...{/a}/{/li} {li}{a href=...}...{/a}/{/li} {ul}
This could be made to work but the file open screen has another trap. I have shown only a single path in the mockup. But there can be multiple paths open simultaneously. There is no clear 1-to-1 correspondence between any variable such as $path and what it expands to.
It looks like my first working template file is going to be defined as
$std_head $file_open $std_foot
where $file_open is defined entirely at runtime by Python code stitching together pieces of html code.
Score one for practice over theory.
Trackback(0)
|
|
Last Updated on Sunday, 25 January 2009 12:41 |