|
Blog -
Program
|
|
Written by Dennis Reinhardt
|
|
Monday, 05 January 2009 16:08 |
|
I have been contemplating changing from my Python 2.3 standard with HTML User Interface (UI) to some other environment. The leading choices are: - Python 2.3 with templating Html UI
- Python 2.5 with WX UI
- Python 3.0
- Delphi
- .net
One problem I have with my current UI is that Python is not really very good at embedding html. Spaces are part of the Python language and embedding indented html is a mess. I could define a global string and use triple quoting to define a multi-line snippet but variable substitution is painful. What I want to do is collect html snippets into an external file, one that I can review in a web browser and then pull that into my program. I looked though web fameworks and templating systems in Python and did not see what I wanted. The closest, WASP, has a GNU license and I cannot use it in a commercial product. So, I will write my own. The initial specs for expansion of prototype are Prototype expansion ------------------- $name is a text substitution from
1) a template file tpl/name.html
or the following entered into a global dictionary
2) the text value for name in a global dictionary 3) a subroutine call which when made returns text
The regex for $name is [$][a-zA-Z][a-zA-Z_0-9]*
Expansion of the prototype takes place repeatedly until no more substitutions can be made or MAXEXPANSION limit hit.
$name that cannot be expanded are left in the prototype as is. This allows debug and dollar amounts to not require coding.
$_ explicitly escapes to $ and $! to null. Thus Micro$oft decodes to Micro$_oft and $namemore would decode to $name$!more. Explicit escapes imply that escape remains in prototype until all other expansions are made. $_ is done last so that $_! decodes to $!
Using $$ as an escape is difficult in the presence of repeated substitution because the parser must backup a character to see if $name is really $$name. Further, $name1$name2 is difficult to parse for $name2 since we have already substituted $name1.
Next step is to set up a development directory for TextToolKit and prototype this facility. [I have done this and edited the definiiton based on testing] : completely lock the comment area
Trackback(0)
|
|
Last Updated on Monday, 19 January 2009 10:23 |
Python's shortcoming here isn't much of a practical disadvantage because embedding html in code is a poor idea anyway. Best practices in this area include maintaining a reasonably strict separation among content, logic, and layout.
If none of the many Python templating systems are to your liking, you might be well-enough served by using a plain text file that contains html and some %(varname)s insert points. There's no need for triple-quoting... you can just read the file in as text and perform regular string substitutions on it, eg:
file contents (test.txt)
--------------
This %(objname)s is %(status)s
code
----------
>>> template = open('test.txt','r').read()
>>> context = {'objname': 'system', 'status':'working'}
>>> template % context
'This system is working'
That's a usable templating system in almost no code at all. If your needs grow to include display logic, the value of using an existing templating system may increase.