jest has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
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.
For example,
push (@row, Tr( td({-width=>"10%"},u(b("Bottles Remaining"))), td({-width=>"*"},u(b("Wine"))) )); while (my $ref = $sth->fetchrow_hashref()) { my $country = escapeHTML($ref->{'country'}); if ($country ne $oldcountry) { push (@row, Tr(td({class=>"regioncount"}," $regioncount Total for + region"))) unless $regioncount == 0; # skip first pass push (@row, Tr({class=>"countrycount"},td("$countrycount Total fo +r country"))) unless $countrycount == 0; # skip first pass push (@row, Tr(td({class=>"countryheader"},$country))); $countrycount = 0; $oldregion = "bar"; # clear region when country changes } $oldcountry = $country; my $region = $ref->{'region'} ? escapeHTML($ref->{'region'}) : "Othe +r"; if ($region ne $oldregion) { push (@row, Tr(td({class=>"regioncount"}," $regioncount Total for + region"))) unless ($regioncount == 0) or ($countrycount == 0); # skip first + pass push (@row, Tr(td({class=>"regionheader"},$region))); $regioncount = 0; } $oldregion = $region; $regioncount += $ref->{'number_left'}; $countrycount += $ref->{'number_left'}; $totalcount += $ref->{'number_left'}; my $wine_name = a({-href=>"wine_display.cgi?id=$ref->{id}"}, escapeHTML($ref->{'wine_name'})); push (@row, Tr({-class=>"$rowcolor"}, td({-class=>"body"},$ref->{'number_left'}), td($wine_name) )); $rowcolor = ($rowcolor eq "lightrow" ? "darkrow" : "lightrow"); } push (@row, Tr({class=>"regioncount"},td("$regioncount Total for regi +on"))); push (@row, Tr({class=>"countrycount"},td("$countrycount Total for co +untry"))); push (@row, Tr({class=>"totalcount"},td("$totalcount Total in cellar" +))); print table({-width=>"95%", -cellspacing=>"0"},@row);
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to understand template-foo
by perrin (Chancellor) on Sep 12, 2003 at 04:00 UTC | |
by jest (Pilgrim) on Sep 16, 2003 at 17:58 UTC | |
|
Re: Trying to understand template-foo
by Roger (Parson) on Sep 11, 2003 at 23:46 UTC | |
|
Re: Trying to understand template-foo
by jdtoronto (Prior) on Sep 12, 2003 at 02:40 UTC | |
|
Re: Trying to understand template-foo
by cfreak (Chaplain) on Sep 12, 2003 at 13:36 UTC | |
|
Re: Trying to understand template-foo
by xtype (Deacon) on Sep 13, 2003 at 21:37 UTC | |
|
Re: Trying to understand template-foo
by idsfa (Vicar) on Sep 16, 2003 at 06:27 UTC |