CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: mod_perl:
HTML::Template not working inside handler

 



xyon
New User

Feb 25, 2008, 11:14 AM

Post #1 of 4 (23987 views)
HTML::Template not working inside handler Can't Post

Hello everyone,

I'm working on coding up a mod_perl handler, but am unable to get HTML::Template to output. Below is my current code:

MyHandler.pm:

Code
# file:MyHandler.pm 
#------------------------------------------------------------------
package MyHandler;

#Load some helpful functions
use strict; #strict tolerance for code
use warnings; #extra warnings in the log
use Carp; #verbose logging
use diagnostics; #more verbose logging

#Loadup some functions for later use
use HTML::Template;

#Loadup functions involved in being a handler
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Request;
use Apache2::Const -compile => qw(OK);

#------------------------------------------------------------------
#
# Subroutines
#

sub handler {
my $hdlr = shift;
$hdlr->content_type('text/html');
$hdlr->print("Handler started<br />");

my $template = HTML::Template->new( filename => "/templates/main.tmpl");
$hdlr->print("outputting template...<br />");
$template->output();

$hdlr->print("after main, now sending Apache 'OK'<br />");
return Apache2::Const::OK;
}
1;


all of the $hdlr->print statements are printed, but the contents of "/templates/main.tmpl" are not printed. I don't see any errors in the Apache logs.


KevinR
Veteran


Feb 25, 2008, 1:20 PM

Post #2 of 4 (23984 views)
Re: [xyon] HTML::Template not working inside handler [In reply to] Can't Post

Specifically I don't know. Try some basic debugging and see if it helps you to find out what the problem is:


Code
my $template    = HTML::Template->new( filename => "/templates/main.tmpl") or die "Can't create a new Template object: $!";


Generally though stuff like this is a path or permissions problem associated with the file, main.tmpl in this case.
-------------------------------------------------


raxip
Novice

Jun 10, 2009, 7:53 PM

Post #3 of 4 (21682 views)
Re: [xyon] HTML::Template not working inside handler [In reply to] Can't Post

As KevinR loosely pointed to, HTML::Template is looking at your filename template as an absolute path (because it begins with a '/'). That is to say.. it's looking for the template off of your root path inside the `templates' directory.

e.g. /templates

If your web server serves files out of the /var/www, then you'll want to use, filename => '/var/www/templates/main.tmpl'.


Zhris
Enthusiast

Jun 15, 2010, 8:41 PM

Post #4 of 4 (19374 views)
Re: [xyon] HTML::Template not working inside handler [In reply to] Can't Post

It doesn't look like you've actually "printed" the template.

Shouldn't:
$template->output();
be:
print $template->output();

Using a relative path to your template is sufficient.

Chris


(This post was edited by Zhris on Jun 26, 2010, 11:58 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives