Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

HTML::Template

by defyance (Curate)
on Mar 24, 2003 at 18:32 UTC ( #245501=modulereview: print w/ replies, xml ) Need Help??

Item Description: A great module for creating HTML templates.

Review Synopsis:

We hear about it all the time, and most, if not all of us have dabbled in it at one time. You know what I speak of, Dynamic web content. Well, if you prefer, or are required to use a template system, might I suggest giving HTML::Template a go. Some may ask my main reason for endorsing it, to which I would respond with simplicity, and flexibility. Hands down, HTML::Template gotta be the simplest way to manage templates. Lets give an example.

Taken from the CPAN docs HTML::Template:

Create a simple HTML page, with two template variables HOME and PATH, name it test.tmpl.
We will fill the variables with data below.

<html> <head><title>Test Template</title> <body> My Home Directory is <TMPL_VAR NAME=HOME> <p> My Path is set to <TMPL_VAR NAME=PATH> </body> </html>

Now, lets build a program to fill in those previously named variables. The result is displayed below

#!/usr/bin/perl -w use HTML::Template; # open the html template my $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters $template->param(HOME => $ENV{HOME}); $template->param(PATH => $ENV{PATH}); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n", $template->output;

Make sure its all ready to go, then point your browser to the script you just created, now, you should have output that looks kinda like this:

My Home Directory is /home/some/directory My Path is set to /bin;/usr/bin
Simple, no?

It Gets Better

Not only does it support simple variables, you can also:

  • Escape HTML from your data!
  • Set default values, if data is not supplied by param!
  • Build Loops!
  • Include other templates!
  • Build Conditionals!
  • Error handling has descriptive error output, which speeds up debugging!
  • And much more!

    I'm not going into much more detail here, I like to keep it simple, just know that I have found it useful. Paired with CGI, DBD::mysql, CGI::Session, and various others, I've been able to create powerful web applications. Another note, I feel that the module is newbie friendly, as I myself is a newb ;-). Further info can be found on CPAN, and the docs are very easy to read. So far, I have no displeasures with this module, please, reply with your opinions.

    Thanks,

    DeFyance

    Comment on HTML::Template
    Select or Download Code
    Re: HTML::Template
    by cchampion (Curate) on Mar 24, 2003 at 18:51 UTC
        You know whats *really* sad? I've read HTML::Template Tutorial by jeffa before, great Tutorial.. My intention was more of a quick, simple review. I'll leave the tutorials to the master(s)!

        -- Can't never could do anything, so give me and inch, I'll make it a mile.

    Re: HTML::Template
    by zby (Vicar) on Mar 25, 2003 at 08:31 UTC
      I like more Template::Toolkit. There are a few reasons but the important one is ergonomy - visibility of the template in the HTML. For example your code:
      <html> <head><title>Test Template</title> <body> My Home Directory is <TMPL_VAR NAME=HOME> <p> My Path is set to <TMPL_VAR NAME=PATH> </body> </html>
      and TT style code:
      <html> <head><title>Test Template</title> <body> My Home Directory is [% HOME %] <p> My Path is set to [% PATH %] </body> </html>
      For me the template variables here are much more visible, while in HTML::Template you need to concentrate a bit to see where the Template is and where the HTML tags are.
        Template toolkit is indeed more ergonomic, but it also carries a lot of (useful) baggage with it (but baggage is still baggage). The example given lends itself to a more simpler solution. Rather than being the Pepsi guy at a Coca-Cola rally, i would have recommended Inline::TT:
        use strict; use warnings; use Inline qw(TT); print test( HOME => $ENV{HOME}, PATH => $ENV{PATH}, ); __END__ __TT__ [% BLOCK test %] <html> <head><title>Test Template</title> <body> My Home Directory is [% HOME %] <p> My Path is set to [% PATH %] </body> </html> [% END %]
        However, i still prefer HTML::Template for its sheer simplicity. And my eyes haven't gone bad yet. ;)

        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)
        
    Re: HTML::Template - complex sites
    by Heidegger (Hermit) on Mar 25, 2003 at 08:58 UTC

      I'm using the HTML::Template in my project. The examples given in the tutorials are pretty simple - one or two tables. However, if you take a normal website, it contains 2-3 menus, a few link boxes, page content, header, footer and other things. So, there is a need to break the homepage into small templates. Here we go: I have ~ 30-50 templates in my project. I'd liked to hear from othe perl monks how they deal with such situation, because I believe most of the websites are pretty complicated.

      I use HTML::Template together with DBIx::Recordset at my work. As I have already noted I have ~ 30-50 templates for different entry forms and website components such like button groups. I use template includes and loops, trying to make them as abstract as possible.

      I'd liked to hear if anyone is writing such modules like Menu.pm that loads its entries from the database and knows how to display its items using the templates. There are many components which I needed for my website and had to write myself. I believe there must be frameworks like that around, but I don't know any.

        I would like to know more about how you are doing things and what you mean by breaking the homepage into small templates.

        I am using a MySQL db to populate variables.

        Also, I'm afraid I'm using H::T to swap out large chunks of HTML, depending on whether a user is entering a new record or editing an old, avoiding using 2 templates, one for each. It's a toss-up: do you want to edit the design in 2 files or make it harder to edit the design in just one, but more complicated file? I don't know.

          Well, HTML::Template is not going to make your database searching any easier, but it will allow you to use abstraction techniques on HTML. For example, i like to use a 'skeleton' template that encapsulates the header, footer, and main content for a site:
          <html> <head> <title><tmpl_var title></title> </head> <body> <tmpl_include name="header.html"> <tmpl_var CONTENT> <tmpl_include name="footer.html"> </body> </html>
          Now, that was just an example ... i don't always use that format, but i hope you get the picture. The idea is that you break up the "elements" of the web site into their own files. Say you have a menu bar on the side. You can abstract the HTML into it's own file and then simply include that file when you need it. As long as code that populates the params of the HTML::Template object passes the parameters that 'widget' needs, all is well. Here is a complete example you can play with:

          One last item. A question for you. What is the difference between two forms - one form adds a new record, the other form edits an existing record? What is really the only difference? The first form does not have an id - the second does. So, if there is no id present, then the user is adding a new record. If there is, then they are editting an existing record. It is really not very hard to abstract this into 3 templates: 2 skeletons (one for edit, one for add) and a template that contains the form elements. I leave this as an exercise. :)

          Oh, and remember, sometimes it is more trouble than it is worth to get as abstract as you possible can. You might wind up making your interface so general that no one can easily use it anymore. :/

          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)
          
    Re: HTML::Template
    by blakem (Monsignor) on Mar 26, 2003 at 01:41 UTC
      It should probably be mentioned that HTML::Template was written by our very own samtregar. ;-)

      -Blake

    Re: HTML::Template
    by bradcathey (Prior) on Sep 01, 2003 at 18:57 UTC
      Maybe I'm late to the wedding, but as a graphic designer, I like the H::T approach of separating Perl from HTML, allowing non-programmers (like clients and employees) to make design changes without opening up a Perl script and editing the HTML there.

      Wish it had a protocol like T::T so that the variables were more distinct visually. I don't know if this would solve the problem BBEdit has with H::T and closing tags (won't do it with H::T stuff on the page).

      Also might be abusing the privilege by assigning large chunks of HTML to the variables to avoid having more templates for a given page (see above).

      All in all, I think H::T is a great solution.

    Back to Reviews

  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (12)
    As of 2014-12-26 20:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (175 votes), past polls