Python vs PHP

has has.temp2 at virgin.net
Mon Oct 25 14:56:27 EDT 2004


Max M <maxm at mxm.dk> wrote in message news:<417cb790$0$226$edfadb0f at dread12.news.tele.dk>...
> 
> The main problem with most templating systems is not only that they mix 
> markup and code, but also that markup reuse is allmost impossible.

[Just to add my 2c...]

These criticisms may well be true of macro-style templating systems,
but neither should be an issue for object model-based systems
(Nevow.Renderer, PyMeld, HTMLTemplate) that are properly designed.
Markup is used to construct the template object model; the code to
manipulate that model is supplied separately. And being constructed of
objects means almost by definition that the template will be reusable
in whole or in part.


> An illustrative example is a "box". [...]
> If you want to reuse this in most teplate systems, you need to do it in 
> the programming language. Eg. in Python like::
> 
> 
> def box(title, content):
>      return """<div class="box">
>      <h5>%s</h5>
>      %s
>      </div>""" % (title, content)

Simple solution: any templating system that makes you write such
nonsense automatically warrants a trip to the bit-bucket.:p Honestly,
I wouldn't waste time pondering it. It's not like these problems
haven't already been solved.


> Zope Page Templates has a far better approach to this imho. Where you 
> define reusable markup in a macro.

Better than nothing perhaps, but having to design, implement and use a
whole new macro system just to solve one particular reuse problem is a
rather expensive solution. (e.g. See GreenSpun's Tenth Rule, and take
a lesson from Lisp which solved these problems fifty years ago.) In an
object-based templating system, reusing one chunk of template in
another would take something like:

template2['box'] = template1['box']

If you also want to retain the content from template2's original 'box'
object as the ZPT macro example does, you can write yourself a
five-line generic 'merge(template1, template2, nodename)' function
that does this automatically and stick it in a reusable support
library somewhere. And all in a templating system that can be
implemented in about 350LOC with a full API shorter than this post. If
there's a benchmark for extensibility and reuse in templating systems,
that's gotta be pretty close. :)

Cheers



More information about the Python-list mailing list