|Don't ask to ask, just ask|
Trying to understand template-fooby jest (Pilgrim)
|on Sep 11, 2003 at 21:07 UTC||Need Help??|
jest has asked for the wisdom of the Perl Monks concerning the following question:
I've previously been writing code on an as-needed basis, mostly handrolled stuff, especially for things like Web access to databases. Recently, as I've been writing more and more of it, and spending half the day either cutting-and-pasting code or just reinventing the wheel every day, I've been looking more seriously at various systems for streamlining this work, including Class::DBI, CGI::FormBuilder (which in particular is impressing the hell out of me so far, I must say), and various templating systems. Yes, I was originally inspired to think about this because of Kake Pugh's How to Avoid Writing Code article at perl.com.
I'm not asking about which templating system I should use; I know there are tons of nodes about that already here, many of which I've found helpful. (I'm most interested in HTML::Template and Template Toolkit.) Rather, what I'm stuck on is sort of the nature of templating itself. Up to now everything I've read and studied has been based on CGI.pm and the like for HTML generation, with everything controlled by Perl in terms of how the format is created. But looking at the examples of templating in action, I'm unsure of what sort of things should be handled in code, and what should be handled by the template.
I have a program which queries a database of wine and prints out the contents of a collection. I go line-by-line with the wines, keeping track of when new countries/regions are encountered so I can change the headers (with CSS), and keeping count of quantities so I can display them as running totals. The code that generates this, starting from a returned database statement handle, looks like this:
There are a number of things I'm unclear about in terms of porting this code to a templating system. On the basic level, there's stuff like how to use CSS with it (the tutorials seems to spend less time than I would prefer describing the use of CSS with these systems, which is difficult because I'm new to CSS too), and how differently this might work with Class::DBI. And there are individual questions, like how you're meant to do stuff like switching the row color on alternate lines.
But on a larger level, how much of this is supposed to handled by the templating system, and how much with Perl? Is this something that's going to end up being two lines of Perl, and the rest handled by Perlish code in the templating system? (This is how it's often done in the example code I've seen.) Or is it going to look very close to how it does now, except with template stuff instead of CGI.pm functions? My personal preference would be to do as much as possible in Perl, because that's what I know and that's what I think would be most portable--it's also why I prefer HTML::Template and TT to things like HTML::Mason. But perhaps that's defeating the purpose of using templates. This is all for personal or small-scale use, I don't have to pass things off to a design staff. But I'd like to get better control over presentation without losing control over my Perl. So how should I be thinking of this in my development as a programmer?