Python vs PHP

Max M maxm at mxm.dk
Mon Oct 25 10:24:33 CEST 2004


Alex Martelli wrote:

> Surely there is no prohibition against the Python code generating all
> the HTML it wants; it doesn't even have to use any template whatsoever
> if it doesn't want to, generating HTML from code is the main thrust of
> stan, somewhat similarly to Quixote.  It's not true that Nevow _is_
> templating: Nevow _has_ templating, as one of many tools -- it just
> happens to be the one tool I think should be used, given the scenarios I
> explained.


The main problem with most templating systems is not only that they mix 
markup and code, but also that markup reuse is allmost impossible.

An illustrative example is a "box". Often seen in the and right hand 
side of websites. Eg. on sites like slashdot. Basically it has a title 
and some content. It usually appears many places on a site, and so is a 
prime candidate for reuse.


It can be defined very simply in html like::

     <div class="box">
         <h5>A title</h5>
         Some content.
     </div>


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)


But now there is allready a problem. It is out of the hands of the 
layouter. A programmer is needed to go in and change the markup. 
Naturally this is a simple example, so a layouter would probably be able 
to do it. But as the templates gets increasingly more complex, it's 
going to be increasingly difficult for the layouter to do any changes.

Another thing is that you cannot call this as markup. You need to use 
programming conventions::

     <% box('My little Box', 'Some content in the Box with <b>bold</b> 
markup')%>

This approach of reuse, while technically correct, is next to useless. 
Even if there is only mildly complex markup as content in the box.

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

The box could then be defined with title and content slots like::


     <div metal:define-macro="box">
         <h5><metal:block define-slot="title">The title</metal:block></h5>
         <metal:block define-slot="content"/>This is example 
content</metal:block>
     </div>


Wich you can resuse like:


     <div class="box" metal:use-macro="box">
         <h5 metal:fill-slot="title">A Title</h5>
         <metal:block fill-slot="content">Some content in the Box with 
<b>bold</b> markup</metal:block>
     </div>


This approach makes it relatively easy to reuse markup as well as logic. 
It is possible use arbitrarily complex markups as "parameters" to the 
slots in the template. Wich makes it very nice for larger systems.


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list