Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to Perl so bear with me on this. I would like to use templates in my Perl programs but I can't quite get them to work. I would like to take the file and put it onto the screen... okay, I can see you scratching your head by now. What I want to do is place variables in the .tmpl file so that when the whole thing is executed, it shows what I want it to show. For instance, when I have the file, my.tmpl, it might look something like this:
<HTML> <HEAD> <TITLE>$Title </title> </head> <BODY> Here's the text from the .txt file<br> @Text </body> </html>
So I could then add something like this to my Perl code.
my $template = HTML::Template->new(filename => 'my.tmpl'); print $template->output; exit;
Yet it doesn't want to work. Can you highly trained Monks help me?

Replies are listed 'Best First'.
(jeffa) Re: About HTML::Template
by jeffa (Bishop) on Nov 12, 2001 at 11:39 UTC
    I was getting ready to explain loops, but then i noticed that you want to "include" another file inside your template. Easy, just change the template:
    <BODY> Here's the text from the .txt file<br> <TMPL_INLCUDE NAME="filename.txt">
    To get $Title in there, you will need to set the param for it in your Perl script:
    $template->param(TITLE => $Title); print $template->output;
    And then inside your template:
    <HTML> <HEAD> <TITLE><TMPL_VAR NAME="TITLE"></TITLE>
    If you actually want to start processing arrays and hashes, you will need to get more sophisticated. Check out a tutorial.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: About HTML::Template
by Asim (Hermit) on Nov 12, 2001 at 12:03 UTC

    First, drop everything and go read jeffa's tutorial on HTML::Template. But the HTML should look something like this (untested model code):

    <HTML> <HEAD> <TITLE> <TMPL_VAR NAME=Title> </title> </head> <BODY> Here's the text from the .txt file<br> <TMPL_LOOP NAME=Text> <TMPL_VAR NAME=Line><br> </TMPL_LOOP> </body> </html>
    But that's a LOT of work. If all you want to do is print a text file to a screen, then just crank up CGI.pm, and print the contents (untested model code):
    use strict; use CGI qw(:standard); print header; print start_html('My Text File'); open (TEXTFILE, "my_text_file") or die "Cannot open text file: $!"; while (<TEXTFILE>) { chomp; print $_, p; } close (TEXTFILE) or die "Cannot close file: $!"; print end_html;
    No need to use HTML::Template to do all that work.

    ----Asim, known to some as Woodrow.

Re: About HTML::Template
by tomhukins (Curate) on Nov 12, 2001 at 15:54 UTC

    Other replies have covered most of what I'd suggest, but so far nobody has mentioned that you're not checking for and reporting errors.

    If your code isn't working, use Perl's error reporting functionality to find out why. Try this:

    my $template = HTML::Template->new(filename => 'my.tmpl') or die "Cann +ot create template object: $!"; print $template->output() or die "Cannot output template: $!";

    If either of the methods you're calling fail, you'll be able to find out why. If you're running from the command line, these errors will be output on your terminal. If you're running as a CGI, the errors will be output to your server error log (or you could use CGI::Carp's fatalsToBrowser to output errors in the HTTP response).

Re: About HTML::Template
by cfreak (Chaplain) on Nov 12, 2001 at 21:54 UTC

    As well as the tutorials mentioned above I'd also look at the HTML::Template manpage. Its very detailed and useful. As far as the code you have here in order to set a template variable you have to use a param call like this

    $template->param(some_var_name=>'foo');

    Then in your template file something like this:

    <html><body> <tmpl_var name="some_var_name"> </body> </html>
    After running you would get a page that said 'foo'. Hope that helps

    Chris