[Tutor] separating logic from html in web apps

Alexandre Ratti alex@gabuzomeu.net
Wed, 29 May 2002 10:49:09 +0200


Hello Chris,


At 21:45 28/05/2002 -0400, you wrote:
>Date: Tue, 28 May 2002 16:57:16 -0800 (AKDT)
>From: Chris Lott <chrisl@frodo.thethirdsector.com>
>Subject: [Tutor] separating logic from html in web apps

>What programming I know has largely been in Cold Fusion, PHP and Perl
>for system admin type tasks and some basic form CGI.I have repeatedly
>been told how desirable it is to use a programming language like Perl
>or Python that allows one to separate code from HTML in web
>applications.

I'm writing a Web app as a learning project; in this app, I try to put as 
little logic as possible in the HTML templates. Basically, I prepare data 
as much as possible at the Python level and I use templates for display 
only. These templates may still contain loops and conditions.

I use the Cheetah Python templating system:

         http://www.cheetahtemplate.org/

>For example, let's say that I have a db and I want to publish it in
>five different formats for HTML, XML, WAP, RDF and who knows what
>else. In the PHP way, I would have five different templates, or one
>template with five conditional blocks, each of which output one of
>these versions.

I would also use 5 different templates and customize data as much as 
possible at the Python level. Also, with an object-oriented templating 
system, you might try and write a general template and specialize it 
through inheritance.

>I still have to have five different conditional blocks or objects or 
>something to create the output, I've lost the utility to edit HTML in an editor
>(which is nice occasionally)

If you use templates, you can edit HTML in text editors and possibly in 
WYSIWYG editors. However, I would store as little logic as possible in the 
HTML code. For instance, I would not store data access code, SQL queries 
code, etc. right in the HTML pages. I'm not sure how it is usually done in 
PHP.

Still, I find it complex to fully separate logic and HTML. In some case, 
it's easier to generate HTML from Python, instead of inserting values in 
HTML templates. In my app, I ended up doing both. Ideas are welcome.


Cheers.

Alexandre