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 MrCromeDome (Deacon)
on Jun 16, 2004 at 16:58 UTC ( [id://367304]=note: print w/replies, xml ) Need Help??


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

Frankly, the first two or three projects I did with HTML::Template were not done the right way ;) There were a lot of places where I was generating HTML inside of my Perl scripts because in my head I could make more sense of it there. Another project came along later. . . was writing a fully-featured demo to try and land a big contract for my company, and I knew my meager design skills wouldn't cut it, so I needed a real web page designer. The one I liked wouldn't know Perl if it bit her in the ass ;) Sooooo, my poor practices had to stop. I gave her simple text files with the variables I needed in them. . .

. . .And I've never looked back since. It takes some work sometimes, but my content and program logic are entirely separate now. I can hand my designer blank template files, write my programs, and know they will work perfectly and look great when the templates come back.

How do I do it??? Liberal use of some of the more advanced features of H::T, lots of TMPL_IFs, TMPL_VARs, and TMPL_LOOPs, and the good ole' conditional operator ;) I'm not saying this is the best way, but it works well for me.

Here's a brief example of what I do. First, the template:

<table border="0" cellpadding="0" cellspacing="3"> <tr valign="top"> <td> <b><a href="<!-- TMPL_VAR NAME=SCRIPT -->?<!-- TMPL_IF NAM +E=SESSION -->CGISESSID=<!-- TMPL_VAR NAME=SESSION -->&<!-- /TMPL_IF - +->mode=results&sort=parcel&dir=<!-- TMPL_VAR NAME=PARCEL_DIR -->&chan +ge=true">Parcel Number</a></b> <!-- TMPL_IF NAME=PARCEL_ASC --> <img src="/images/arrow_up.gif"> <!-- /TMPL_IF --> <!-- TMPL_IF NAME=PARCEL_DESC --> <img src="/images/arrow_down.gif"> <!-- /TMPL_IF --> &nbsp; </td> <td> <b><a href="<!-- TMPL_VAR NAME=SCRIPT -->?<!-- TMPL_IF NAM +E=SESSION -->CGISESSID=<!-- TMPL_VAR NAME=SESSION -->&<!-- /TMPL_IF - +->mode=results&sort=name&dir=<!-- TMPL_VAR NAME=NAME_DIR -->&change=t +rue">Name</a></b> <!-- TMPL_IF NAME=NAME_ASC --> <img src="/images/arrow_up.gif"> <!-- /TMPL_IF --> <!-- TMPL_IF NAME=NAME_DESC --> <img src="/images/arrow_down.gif"> <!-- /TMPL_IF --> &nbsp; </td> <td><b>Address</b></td> </tr> <!-- TMPL_LOOP NAME=RESULTS --> <tr valign="top"> <td><font color="<!-- TMPL_VAR NAME=COLOR -->"><a href="<!-- T +MPL_VAR NAME=SCRIPT -->?mode=view&parcel=<!-- TMPL_VAR NAME=UPARCEL - +->&year=<!-- TMPL_VAR NAME=YEAR -->"><!-- TMPL_VAR NAME=PARCEL --></a +></font></td> <td><font color="<!-- TMPL_VAR NAME=COLOR -->"><!-- TMPL_VAR N +AME=NAME --></font></td> <td><font color="<!-- TMPL_VAR NAME=COLOR -->"><!-- TMPL_VAR N +AME=ADDRESS --></font></td> </tr> <!-- /TMPL_LOOP --> </table>
And now the Perl:
$tmpl_results->param ( RESULTS => \@results, SESSION => $config{USE_SESSION_HACK} eq "Y" ? $self->param("se +ssion")->id : "", SCRIPT => $request->url, SORT => $sort, PARCEL_DIR => (($sort eq "parcel" and $dir eq "ASC" ) ? "DESC" : +"ASC"), PARCEL_ASC => (($sort eq "parcel" and $dir eq "ASC" ) ? "Y" : "") +, PARCEL_DESC => (($sort eq "parcel" and $dir eq "DESC") ? "Y" : "") +, NAME_DIR => (($sort eq "name" and $dir eq "ASC") ? "DESC" : +"ASC"), NAME_ASC => (($sort eq "name" and $dir eq "ASC" ) ? "Y" : " +"), NAME_DESC => (($sort eq "name" and $dir eq "DESC" ) ? "Y" : " +"), DIR => $dir, TIME => sprintf("%$config{FORMAT_TIME}", $time), NUM_RESULTS => $rows, START => $first, END => $last, FIRST => ($first == 1 ? "" : "first=1&last=$config{INCREMENT +}"), PREV => ($first == 1 ? "" : "first=" . ($first - $config{IN +CREMENT}) . "&last=" . ($last - $config{INCREMENT})), NEXT => ($last >= $rows ? "" : "first=" . ($first + $config +{INCREMENT}) . "&last=" . ($last + $config{INCREMENT})), LAST => ($last >= $rows ? "" : "first=" . ($rows - $config{ +INCREMENT}) . "&last=$rows"), );
One thing that has drastically helped my development is HTML::Template. It promotes cleaner application structure, and plays nicely with H::T. Give it a check.

Feel free to contact me with questions about what I did.

Cheers!
MrCromeDome

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found