[Baypiggies] Output generation - (string operations/triple quote/template)

Chris Clark Chris.Clark at ingres.com
Wed Feb 28 19:55:31 CET 2007


Sorry for the really late reply but I've been having posting problems :-(


Bob Ippolito wrote:
> On 2/13/07, jwithers <grayarea at reddagger.org> wrote:
>   
>>> I agree with JJ that template language is another good alternative,
>>> especially for more complex generation. While this is subjective and
>>> everyone have different criteria, I consider the approach of interleaving
>>> Python code with the target output hard to maintain. It goes back to the
>>> Java Servlet approach and the many addon projects later to provide better
>>> alternatives.
>>>       
>> I have found TAL to be good for taking care of that. Templates, but you
>> are close to forced to move your code outside the templates. Most people
>> don't seem to like it much, but it makes me happy.  Most of Zope gets on
>> my nerves in one way or another, but TAL makes for better separation
>> than the majority of templating systems, and avoids the ugly of building
>> your html inside your code.
>>     
>
> Genshi offers the same paradigm as TAL but uses Python syntax and is a
> bit friendlier to those who want to put code in templates. It's a
> pretty good middle ground in my experience.
>   

Templates are definitely a personal (and project) preference and 
discussions can easily become the next "My dog-shaker is better
than your dog-shaker...". The usual principle of using the right tool
for the right job arises. The main points I looked at when I was looking
for a templating system where:

    * I really wanted to have all code outside of the template, there
      are times when this is useful but I wanted seperate. E.g.
      something TAL like.
    * had to handle Unicode in some form (encoding type really wasn't an
      issue)
    * Create html (i.e. handle html escaping for me), xml/xhtml would
      have been nice but I really needed html
          o I wanted to use a html editor - I wanted/needed to hand
            craft some of the tags but for the most part I wanted to use
            http://www.nvu.com so I didn't have to hand craft
            everything. This meant the replacement indicators had to be
            preserved by my (dumb) html editor that know nothing about
            templating
    * I also needed to bolt in an off the shelf javascript library for
      real time updates of the dom. The library I was using was small
      but did add elements to the original html that wasn't 100%
      compliant so I actually needed to handle bad html
    * Small library!
    * Easy to use library!

Other projects (and personal) requirements will be very different from
mine. The "bad html" kept me from using Genshi, but I do like Genshi a lot.

After lots of experimentation I ended up using
http://freespace.virgin.net/hamish.sanderson/htmltemplate.html - one
thing that annoys me slightly about HTMLTemplate is that you have to
write code to populate the template. I was able to get around that in a
few projects by writing a generic routine to take in a dictionary (like
most templating systems). For the few times that I couldn't use my
generic dict system I ended up with a routine (or routines) that have 1
line per item to be replaced in the template. For large numbers of
elements this was easy to maintain, if you have very few elements this
actual is frustrating and you wonder why you used a template :-)

I'm hopefully that I will be using HTMLTemplate for a while, but I can
envisage a project having different requirements which may mean another
templating system! I'm really going to have to invest in some time in
buffet - http://projects.dowski.com/projects/buffet

Just my 2 cents.

Chris




More information about the Baypiggies mailing list