The one thing that really "won me over" was the WRAPPER directive. I so want this feature in HTML::Template, but i fear that some heavy lifting will be required to add it ... anyways, WRAPPER allows you to contain a "header" and a "footer" in one file. This is incredibly useful. Imaging that you have a skeleton ... oh wait a second! I've already explained this once before to you at Re: HTML::Template question. :D

Go back and look at that example again. You may ask yourself, "well, there is a complete script for the H::T example, but where is the code for TT the example?" First, my apologies for not explaining ... second, try this:

  1. save the first template as skeleton.html
  2. save the two pages as page1.html and page2.html, respectively
  3. type tpage page1.html in your shell
If you have all the TT2 stuff installed correctly that should work just fine. Neat, eh?

If you want a skeleton Perl script to work with, try this one on for size:

use DBI; use Template; my $dbh = DBI->connect( ... ); my $sth = $dbh->prepare('select id,title from movie'); $sth->execute; my $movies = $sth->fetchall_arrayref({}); my $template = Template->new; $template->process(\*DATA, {movies => $movies}) or die $template->error(); __DATA__ <form> <select name="movies"> [% FOR movie = movies %] <option value="[% movie.id %]">[% movie.title %]</option> [% END %] </select> </form>
It is practically the same code, but the template is quite different. Where TT greater differs from H::T is when i refer to [% movie.title %] -- movie doesn't have to just be an anonymous hash reference in a list, it could be a full-blown object in a list. Likewise, title doesn't have to just be a hash key, it could be a method of a movie object as well!

I am completely sold on TT ... but it is slower and requires programmers who have quite a bit of experience with Templates in general. There are a lot of things that you can do in TT that you probably shouldn't. For example, here is another TT example:

use Template; my $template = Template->new; $template->process(\*DATA) || die $template->error(); __DATA__ [% USE DBI( ... ) %] <form> <select name="movies"> [% FOR movie = DBI.query('select id,title from movie') %] <option value="[% movie.id %]">[% movie.title %]</option> [% END %] </select> </form>

HTML::Template is simpler and faster, but TT is more fun and extremely powerful. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to 3Re: HTML::Template, DBI and <TMPL_LOOP>'s by jeffa
in thread HTML::Template, DBI and <TMPL_LOOP>'s by jdtoronto

Title:
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, details, 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, summary, 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.