HTML::Template module in perl

A

andy

I have a html file name test.html as below.
==================================================================
<html>
<head>
<title>Song Listing</title>
</head>
<body>
<h1>My Songs</h1>

<tr>

<td valign="top">
<table width="150%" border="1" cellpadding="3"
cellspacing="1">
<tr height="20" style="font-size: 95%;"><td>&nbsp;</td></tr>

<tr height="20" style="font-size: 95%;"><td align="left"
valign="middle"><a href="<TMPL_VAR NAME=SOME_DATA>">NITIN</a></td></
tr>

<tr height="20" style="font-size: 95%;"><td align="left"
valign="middle"><a href="<TMPL_VAR NAME=TEST_THIS_1>">SITE</a></td></
tr>

</table>
</td>

<table>
</BODY>
</HTML>
======================================================================
I am opening this html file using the below perl script.
======================================================================
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Session;
use HTML::Template;
my $site = "http://www.google.co.in";
my $FILENAME="/usr/nitin/test_123.tmpl"
my $cgi = new CGI;
print $cgi->header();
my $template = HTML::Template->new(filename => "/usr/nitin/
test.tmpl");
#tried with all the below three options: as in the link of SITE of the
html page i want to open a different template file named
test_123.tmpl.
#my $FILENAME= HTML::Template->new(type => 'filename', source => "/usr/
nitin/test_123.tmpl");
#my $FILENAME= HTML::Template->new( filename => 'test_123.tmpl', path
=> [ '/usr/nitin/test_123.tmpl']);
#my $FILENAME= HTML::Template->new_file('test_123.tmpl', filename => '/
usr/nitin/test_123.tmpl');

$template->param(SOME_DATA=> $site);
$template->param(TEST_THIS_1=> $FILENAME);
print $template->output;
1;
=====================================================================

I am not able to open the test_123.tmpl file by using opening this
using this method.

Tried with TMPL_INCLUDE also. But not able to open the same.

Can anybody help me with the solution.
============================================================
After this I want to first click on NITIN and then on SITE. And then i
want a new page or template file . i.e after clicking on NITIN and
then SITE I want to open a new template file. Will it be done by
TMPL_IF tag under HTML::template module or is there any other way to
do that.


Kindly help me with the logic.
 
M

Mark Clements

andy said:
I have a html file name test.html as below.
==================================================================

<snip>

So the file is called "test.html"
======================================================================
I am opening this html file using the below perl script.
======================================================================
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Session;
use HTML::Template;
my $site = "http://www.google.co.in";
my $FILENAME="/usr/nitin/test_123.tmpl"
my $cgi = new CGI;
print $cgi->header();
my $template = HTML::Template->new(filename => "/usr/nitin/
test.tmpl");

You're then trying to open it as "test.tmpl".
#tried with all the below three options: as in the link of SITE of the
html page i want to open a different template file named
test_123.tmpl.
#my $FILENAME= HTML::Template->new(type => 'filename', source => "/usr/
nitin/test_123.tmpl");
#my $FILENAME= HTML::Template->new( filename => 'test_123.tmpl', path
=> [ '/usr/nitin/test_123.tmpl']);
#my $FILENAME= HTML::Template->new_file('test_123.tmpl', filename => '/
usr/nitin/test_123.tmpl');

$template->param(SOME_DATA=> $site);
$template->param(TEST_THIS_1=> $FILENAME);
print $template->output;
1;

It's important to check your return values.


F:\Documents and Settings\Mark3>cat template.pl
use strict;
use warnings;

use HTML::Template;

my $filename = shift;

my $template = HTML::Template->new( filename => $filename ) or die $!;

print $template->output();

F:\Documents and Settings\Mark3>perl template.pl notexists.tmpl
HTML::Template->new() : Cannot open included file notexists.tmpl : file
not found. at F:/Perl/site/lib/HTML/Template.pm line 1658

HTML::Template::_init_template('HTML::Template=HASH(0x18c249c)') called
at F:/Perl/site/lib/HTML/Template.pm line 1238
HTML::Template::_init('HTML::Template=HASH(0x18c249c)') called
at F:/Perl/site/lib/HTML/Template.pm line 1124
HTML::Template::new('HTML::Template', 'filename',
'notexists.tmpl') called at template.pl line 8

F:\Documents and Settings\Mark3>echo test template > exists.tmpl

F:\Documents and Settings\Mark3>perl template.pl exists.tmpl
test template
 
J

J. Gleixner

andy wrote:
[...]
#my $FILENAME= HTML::Template->new(type => 'filename', source => "/usr/
nitin/test_123.tmpl");
#my $FILENAME= HTML::Template->new( filename => 'test_123.tmpl', path
=> [ '/usr/nitin/test_123.tmpl']);
#my $FILENAME= HTML::Template->new_file('test_123.tmpl', filename => '/
usr/nitin/test_123.tmpl');

$template->param(SOME_DATA=> $site);
$template->param(TEST_THIS_1=> $FILENAME);
print $template->output;
1;
=====================================================================

I am not able to open the test_123.tmpl file by using opening this
using this method.

perldoc -f open
Tried with TMPL_INCLUDE also. But not able to open the same.

Can anybody help me with the solution.
============================================================
After this I want to first click on NITIN and then on SITE. And then i
want a new page or template file . i.e after clicking on NITIN and
then SITE I want to open a new template file. Will it be done by
TMPL_IF tag under HTML::template module or is there any other way to
do that.

You have to 'open' a file for write. TMPL_INCLUDE will insert the
contents of a pre-existing file.
Kindly help me with the logic.

Use HTML::Template to populate predefined values in a file/template.
Use an editor, or 'open' a file in your CGI, or other methods to
create a file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top