Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Code and html separation - always or mostly doable? by MrCromeDome
in thread Code and html separation - always or mostly doable? by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found