Python vs PHP

Cliff Wells clifford.wells at comcast.net
Mon Oct 25 16:39:31 EDT 2004


On Mon, 2004-10-25 at 10:24 +0200, Max M wrote:
> 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.

I'm not certain which templating systems you are referring to.  Markup
reuse is certainly a major point for the use of these system.  If the
system isn't capable of this then it isn't much of a templating
system.  

> 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)

Why?  

Since I mentioned Smarty before, I'll give an example in that templating
system.  Your example:

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

would become the following Smarty template:

{* box.tpl *}
<div class="{$class}">
  <h5>{$title}</h5>
  {$content}
</div>

and the following PHP:

<?php
$smarty = new Smarty;
$smarty->assign('class', 'box');
$smarty->assign('title', 'A title');
$smarty->assign('content', 'Some content');
$smarty->display('box.tpl');
?>

Reusing this template on different pages is a simple matter of including
it in other templates:

{* page one *}
<html>
{include file="box.tpl"}
</html>

Additionally, the template itself can define variables to be passed to
the included template, if so needed:

{* page two *}
<html>
{include file="template.tpl" class="box" title="A title" content="Some
content"}
</html>

This is quite possibly *the most basic* use of Smarty (and templates in
general, IMO), so your claim that templating systems don't support this
seems quite bizarre to me.

> 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.

For whatever "templating" system you are referring to, I'd certainly
agree this is useless.  The very term "template" implies the opposite of
what you are demonstrating:

See entry 2a:
http://dictionary.reference.com/search?q=template 

Regards,
Cliff

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list