[Tutor] separating logic from html in web apps

Michael Lamertz mike@lamertz.net
Wed, 29 May 2002 12:09:43 +0200


On Tue, May 28, 2002 at 04:57:16PM -0800, Chris Lott wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> 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.

Separating the content from the presentation is a design decision, and
not really dependend of the underlying language.

> This is very counter-intuitive to me (probably because of the tools I
> learned with).

Different thinking perhaps, but not counter-intuitive.  Look below...

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

And here's the point:  Imagine modifying your database model.  Now you
have to update 5 sections of code.  Besides of that, all your pages are
a mess since they contain code blocks for 5 different languages.

Once you start figuring out the common parts of a page

    * Process query
    * Fetch data from database
    * Manage usersession
    * Whatelse...

which are completely independend of the media you're viewing, you can
pull that functionality out of your pages and encapsule it somewhere
else.  Now you have common code that needs only to be modified at one
spot if anything changes in your concepts.

Now, if you're using a more or less strong templating system - of which
are a vast variety available for all kind of languages - the only thing
left to do is to tell it which template to feed the data into and you're
done.

> What would I save using Python and CGI or mod_python? 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) or to validate the code as it sits, and I
> have to go through the process of escaping special characters for all
> of my HTML output...

Nope, you're still thinking in mixing code and HTML.  First you had code
embedded in HTML - the PHP-Way (tm) - now you just want to turn this
around, but that's not what's meant here.

The code really does only the thing that needs to be done in code:

    * Calculations
    * Database access
    * Session management
    * ...

All the rest is done in a template system.  Since you already know PHP,
think of it like this (Caveman's approach to templating):

Put all your preparation code into an include file which you load at the
beginning of each page.  Then try to do as much in plain HTML as you
can, with the exception of stuff like loops and variable insertion.  If
you feel the need to program more complex stuff put that again into the
include file, since you most likely will need it for the other output
formats too.

> I don't get why it is better except in THEORY. But many things in
> theory don't prove to be so in the real world. I am sure this is just
> a misunderstanding on my part!

Perhaps I've made it a bit clearer?

PS: I intentionally avoided python here since your problem is IMO
completely generic and language independend.  Give it a try with the
tools you're used.  If you feel you have grasped the concept and still
think you need to change the framework take a look at options like Perl
and Python...

-- 
		       If we fail, we will lose the war.

Michael Lamertz                        |      +49 221 445420 / +49 171 6900 310
Nordstr. 49                            |                       mike@lamertz.net
50733 Cologne                          |                 http://www.lamertz.net
Germany                                |               http://www.perl-ronin.de