Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #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.


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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (2)
    As of 2021-05-09 06:32 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (100 votes). Check out past polls.