PHP vs. Python/comp.lang.php?

Suchandra Thapa ssthapa at classes.cs.uchicago.edu
Sat Dec 1 23:14:18 CET 2001


Kragen Sitaker <kragen at pobox.com> wrote:
>>     By templating features, I was refering to a way to separate the html
>> and code into separate files, one for the html with tags indicating where
>> to substitute values and one for the code.  This is primarily so that
>> the web designers/graphic artists can fiddle around with the look and feel
>> without having to know php or python.  Likewise, the developers can write
>> the code without having to worry about the appearance.
>
>Doesn't PHP let you call routines defined in another file?  That sounds
>to me like exactly what you're asking for. And Python certainly lets
>you %-interpolate into strings read from another file.
>
>Of course, neither of these really answer when you have blocks of HTML
>that repeat under programmatic control, unless they're in a third
>file; but I can't think of a reasonable solution to that.

    It's not quite so difficult.  Create a template class that is initialized
with a template file and that lets you control substitutions and loops.  
For example, the template files for the class I wrote in php look like this:

<html>
<head>
 <title><!-- PHP_TITLE --></title>
</head>
<body>
  <table>
  <tbody>
  <tr>
    <td>Name</td>
    <td>Address</td>
  </tr>
  <!-- PHP_BEGIN_LOOP1 -->
  <tr>
    <td><!-- PHP_LOOP1_NAME --></td>
    <td><!-- PHP_LOOP1_ADDRESS --></td>
  </tr>
  <!-- PHP_END_LOOP1 -->
  </tbody>
  </table>
</body>

Then in the code you would do something like this:

$template = new Template('foo.template');
$template->replaceVariable('TITLE', $title);
$template->findLoop(1);
while($i < 10) {
    $template->startLoop(1);
    $template->replaceLoopVariable('NAME', $name[$i]);
    $template->replaceLoopVariable('ADDRESS', $address[$i]);
    $template->writeLoop(1);
}
$template->endLoop(1);
$template->printTemplate()

There is no need to have a third file for loops, and it lets me
separate the html and code very cleanly.  The .php files have 
almost no html in them.  The class in php is just 81 lines and it
wouldn't be very difficult to write this in python.  
    Getting back to the topic, php and python don't really have facilities
to do similar things in the standard library without writing your own code.
Thus the fact that php makes you mix code and html isn't a valid point of
comparision since you need to do the same in python.

    
 

-- 
----------------------------------------------------------------------------
	   		              |
Suchandra Thapa                       | "There are only two kinds of math . 
s-thapa-11 at NOSPAMalumni.uchicago.edu  | books. Those you cannot read beyond 
			              | the first sentence, and those you 
			              | can not read beyond the first page."
			              |                     -C.N. Yang
----------------------------------------------------------------------------



More information about the Python-list mailing list