[Web-SIG] HTMLTemplate
aurora
aurora00 at gmail.com
Fri Jun 3 06:51:48 CEST 2005
On 6/2/05, Ian Bicking <ianb at colorstudy.com> wrote:
> If there's no code then it's not a presentation layer. It's an HTML
> layer, nothing more, just dumb data. Presentation requires logic.
>
> From that perspective, I think doing proper model/presentation
> separation using something like HTMLTemplate requires discipline and the
> addition of another Python presentation layer.
Precisely. I find this one of the biggest advantage of HTMLTemplate.
You don't have to learn yet another half baked scripting language to
build the presentation logic. You just use Python.
So how to structure the model/presentation in HTMLTemplate's case?
Here is what I have done:
foo.cgi - application logic
fooView.py - presentation logic
fooView.html - template
fooView would have the presentation logic. It is mainly methods like
renderXXX(). Trust me it is really straightforward. In reality I find
most of my model and view are one-one corresponding. So I even cut
some corner and merged foo.cgi and fooView.py into one file ;-)
Some of you find the callback API a little convoluted or clumsy. After
using it for a while I think I have found the way to work efficiently.
I'm going to get down on some detail here. Hopefully even people never
seen HTMLTemplate can have a glimpse of how it works.
First you parsed the template. The template object have a wonderful
(diagnosis) method called structure(), which dump the view of the
tree. It helps a lot in coding the presentation logic. For example, my
template has:
root
con:title
con:query
con:error_msg
con:num_match
con:item_from
con:item_to
con:item_total
rep:match_item
con:address
con:description
con:archive_link
con:host
con:page_nav
con:goto_prev
rep:goto_page
con:goto_next
You'll need 3 render methods, one for the root and 2 for each rep node.
render()
render_match_item()
render_goto_page()
In each method, you are going to fill out the child items. For
example, in the main render() method, you should probably fill
title
query
error_msg
num_match
num_match.item_from
num_match.item_to
num_match.item_total
page_nav?
page_nav.goto_prev
page_nav.goto_next
Once I discovered the structure() method writing the presentation
logic becomes natural.
----------------------------------------------------------------------
Let me cast another perspective to show why I like HTMLTemplate so
much. Cheetah's user guide have about 100 pages. Enhancement and new
features are probably on the way. HTMLTemplate's documentation consist
of one short file & a few examples. That's probably all you'll ever
need. It is finished. No significant enhancement is expected. I think
that says a lot about its conceptual complexity and completeness.
Wai Yip
More information about the Web-SIG
mailing list