Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Code and html separation - always or mostly doable?

by DaWolf (Curate)
on Jun 16, 2004 at 18:22 UTC ( [id://367360]=note: print w/replies, xml ) Need Help??


in reply to Code and html separation - always or mostly doable?

I'd like to add my humble 2 cents here:

I've came from a HTML background so this node really drew my attention.

I think that although is possible to do a clean separation of presentation and logic, sometimes it's not the best way to do it, because you end up messing with the readability of the page. Let me clarify this with a (silly) example:

Let's say that a user of your web system should see a red background if he/she has a "0" level or a blue background if he/she has a "1" level.

So, you could do this in two different ways (i'm coding this in a freely, lazy way so dont evaluate my code, this is just an example):

First way (separating logic and presentation):
if ($u_level == 0) { $bgcolor = "#FF0000"; } else if ($u_level == 1) { $bgcolor = "#0000FF"; } # tons of code... print<<<HTML <HTML> <HEAD> <TITLE> DaWolf's silly example </TITLE> </HEAD> <BODY BGCOLOR="$bgcolor"> <!-- tons of content... --> </BODY> </HTML> HTML;
Second way (mixing logic and presentation):
# tons of code... print<<<HTML1 <HTML> <HEAD> <TITLE> DaWolf's silly example </TITLE> </HEAD> HTML1; if ($u_level == 0) { print "<BODY BGCOLOR=\"#FF0000\">"; } else if ($u_level == 1) { print "<BODY BGCOLOR=\"#0000FF\">"; } print<<<HTML2 <!-- tons of content... --> </BODY> </HTML> HTML2;
The advantage of the second way in my point of view is that you don't need to go all the way up to understand what $bgcolor means and why it have this or that value, so the code is more easily understandable and more practical to debug.

That's why I think separation should be avoided unless you are not used to deal with HTML.

As I've stated before, this is just my humble opinion.

UPDATE:

Quick explanation: I've never used a Templating system with Perl, so I can't really give my opinion on that, but I've just tried to show the best way ***for me*** to code my pages =:c)

Best regards,

my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Replies are listed 'Best First'.
Re^2: Code and html separation - always or mostly doable?
by cfreak (Chaplain) on Jun 17, 2004 at 04:12 UTC

    I can see $bgcolor and know exactly what it means. Same with any other well-named variable. That aside there are tons of problems with your approach

    1. What happens when you need to make a really large page? You can't see $bgcolor if its buried under 1000 lines of lines of HTML. You also will find it much harder to see anything else in your program.
    2. Designers don't know and don't want to learn Perl. They want to make HTML in Dreamweaver and then have you make it work. Templating makes this much easier and I can write the program while they're making pretty pictures.
    3. Much less typing! No need for the extra print statements! Design with an HTML editor if you want to. Or as I stated above another designer can work on it.
    4. This code isn't modular, you can't reuse the HTML design easily without copying it to another script. Templates make reuse simple

    No offense intended and I understand its your opinion, however your suggestion is the exact opposite of what the parent poster is trying to accomplish. So its not exactly helpful. Also try some templating, trust me, its not hard (I personally like H::T!) and once you get it you'll never ever go back!

    Update: Fixed some typos and my terrible spelling :)

Re^2: Code and html separation - always or mostly doable?
by kiat (Vicar) on Jun 17, 2004 at 05:29 UTC
    Thanks, DaWolf!

    I used to do it that way. There's a lot of html in the code. In the long run, it might be better to move the bulk of the html to a template.

    You might want to seriously consider something like HTML::Template. I started using it at the beginning of this year and there's no turning back.

    I've moved out lots of html from the code into the templates, and it makes for a cleaner code and probably an easier maintanence task.

    What I find difficult and sometimes impossible to achieve is a 100% separation of code and html. But as others have pointed out, it's doable with a clear idea of the program logic. With time and more practice, I might be able to do that.

    On a different note, I've seen rather huge programs (ikonboard and yabb) with code and html glued together.

    cheers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://367360]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found