good chemistry is complicated,
and a little bit messy -LW
Re: Separation of content and codeby Sherlock (Deacon)
|on Jun 18, 2001 at 15:10 UTC||Need Help??|
All Hail the great HTML::Template! *grin* I don't know if I'd go that far, but I use HTML::Template almost every time I write a CGI script that needs to generate HTML. It has a number of benefits that I find very helpful.
Reduces "Code Bloat"
When I'm writing a CGI script, I find that, if I try to incorporate the HTML within the script itself, the script becomes very long and difficult to deal with. Even if I know exactly what's wrong with the script and how to fix it, I might still end up taking some time sifting through my script trying to find that part of my code. You can imagine how much longer it takes me when I don't know just how to fix it. Basically, separating the two allows you to isolate your problems a little easier. If it's a logic issue, look into your Perl script. If it's a display format issue, check out the HTML template file. This way, you're not spending extra time sifting through either one when that's not really where the problem is.
Lightens your work load (potentially)
This one may or may not be true, depending upon your work situation. For me, this used to be very true, but not so now. You see, if you separate your HTML from your Perl, other people (that don't really need to know Perl) can edit the HTML in order to make the output "look better" or "display data more readably" etc. This has the potential of taking some of the work out of your lap. If all of your HTML is generated within your script, anyone that wants to change the output is forced to edit your CGI script directly. This can lead to people breaking your script on you or people constantly asking you to change the way this looks or change the way that looks. Either one is a big nuisance in my book. Like I said, this used to be very true for me - I used to work in an office where I could do the real code development and then "hand off" my work to some web designers who were very good at making my work "look good." Most of these designers didn't know much Perl, but if I separated the code from the HTML, they were able to edit the HTML for me and help me out with my projects. Otherwise, I would have been stuck trying to do all of the development and all of the "prettying" work myself.
Makes scripts more maintainable
This somewhat relates back to the first point, but I feel that it's different enough to warrant a little extra discussion. Imagine you've just written a large CGI script (we'll say about 1KLOC - that's 1000 lines of code for those scoring at home). Let's assume that about half of that is actually script specifically to generate HTML. Now, imagine (if you're like me, you don't have to imagine very hard) that four months after you've written this large script, something breaks or someone asks for some new functionality. Well, I guess it's time to sift through 500 extra lines of HTML generation code to find your error or, on the other hand, maybe you're sifting through 500 lines of Perl script in order to change a heading to bold in the HTML. Either way, after four months away from your script, you'll probably find that it makes much more sense if you're not trying to piece together the whole thing in one file. You can check out this node to see a detailed explanation of this. I once asked the other monks for help on this topic and that node is what they came up with for me.
So that's roughly why I like using HTML::Template to separate my Perl from my HTML. I like to isolate the two so that I keep different things in different places. Granted, you can generate the exact same output by putting all of the HTML generation within the Perl script, but this usually turns out to be a headache for me. I'm not sure exactly what sierrathedog04 means when he/she says that you can't format the data differently in different cases. For cases such as those, HTML::Template offers conditionals so that you can display different things based on an if-test (or a series of if-tests). If you're looking for a very drastic change, you can even switch to a different template file within your script to accomodate.
Hopefully, this helps a little.
Skepticism is the source of knowledge as much as knowledge is the source of skepticism.