Gossamer Forum
Home : General : Perl Programming :

HTML::Template

Quote Reply
HTML::Template
Hi All:

I like using HTML::Template but I have some problems with it.
It works fine but I cannot template pages that are not in the same folder.

For example :- my $template = HTML::Template->new(filename => 'Thanks.htm'); works fine.

However, I would like to do something like this :

my $template = HTML::Template->new(filename => 'http://www.bigboss.com/Thanks.htm');

Is there some other method to doing this.

Thanks
Quote Reply
Re: [TheSafePick] HTML::Template In reply to
I believe you cannot use URLs, so templates must be on the same server (can be on different domains, providing they are hosted on same box) - by prefixing Unix template path to template filename. For example:

my $template = HTML::Template->new(filename => '/usr/home/html/Thanks.htm');

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] HTML::Template In reply to
   
Thanks:

It took me 2 tries to figure that out. My problemn is that I am working with a template that I have purchased. My server would not allow me to run cgi scripts out of the cgi-bin. The htm pages and the javascript files are all in the html folder. I would like to use HTML::Template to get back to the thankyou.htm page. That works fine, but all the other graphics would not show up in the page. The reason is because the links are not absolutely referenced. For example /public_html/picts/pic1.jpg -
/public_html/scroller/scroller1.jpg

Because the cgi-bin folder falls under the public_htm folder

I tried doing this ../picts/jpg to all the links. This worked for some of the links but the page is very complicated and there are over 60 links and to make matters worse there are links in the javascript file.

It looks like when I use HTML::Template starting out in the cgi-bin directory eventhough I am able to template another page in a different directory - the calling directory remains the same

Please any suggetions.

Thanks
Quote Reply
Re: [TheSafePick] HTML::Template In reply to
Best solution is to edit all of the <img src=""> tags - for example (using relative URLs):

<img src="/picts/pic1.jpg">

There are a number of software applications (e.g., HotDog HTML editor that I use) out there that will do this automatically and recursively across multiple files. Should only take a sec.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [TheSafePick] HTML::Template In reply to
Like Dan already mentioned it doesn't matter where images are (besides the cgi-bin) so long as you use a absolute path instead of a relative path. I always put my templates above the doc root and just set the path in H::T accordingly. If you have templates in more than one path H::T will take an arrayref to the path arg to accommodate. Here is something close to what I do. I use CGI::Application so it's not exact but it gives you an idea:

Code:
sub load_tmpl {
my ($self, $tfile, $args) = @_;

# default param set for the templates
$args ||= {};

# Create our template object
require HTML::Template;
my $t = HTML::Template->new(filename => $tfile,
# Add all your paths here
path => ['/path/to/templates',
'/alternate/path'],
# Associate on objects. I usually associate
# my CGI object by default. ymmv.
#associate => $self->query,

# The usual defaults here:
die_on_bad_params => 0,
loop_context_vars => 1,
global_vars => 1,
);
$t->param(%$args);
return $t;
}

# Then when you need a template object:

my $t = $self->load_tmpl('my_file.tmpl', { some => 'more args here'});
print $t->output;

# And if the template is in a subdir of one of your paths:
my $t = $self->load_tmpl('sub_dir/my_file.tmpl', { some => 'more args here'});
print $t->output;

# or even shorter:
print $self->load_tmpl('my_file.tmpl', { some => 'more args here'})->output;

~Charlie
Quote Reply
Re: [TheSafePick] HTML::Template In reply to
Thanks Guys:

I have solved all the problems by changing all resources to absolute links. Everything work like a charm.

However, it seems like when one problem is solved there's another. At present I am having a problem with my embedded javascript code


sub Validate1
{
print "<html><head>\n";
print "<script Language=\"JavaScript\">\n";
print " alert(\"All required fileds must be filled out\");\n";
print "history.back()\;";
print "</script></head></html>\n"; exit;
}

It seems that the history.back() is not working on my new server. However, it works well on my previous server. That's ok because I am going to place javascript in the form itself. However, I want to use this piece of code.


while( my @v = $dbs->fetchrow_array() )
{
if( uc($vals{name}) eq uc($v[0]) && uc($vals{company}) eq uc($v[1]) && uc($vals{email}) eq uc($v[2]) )
{
Validator::Validate2(\%vals);
last;
}
}

Function Validate2 contains javascript validation code as in Validate1

There is no way I can place this in the from "Validate2" because I am coming from server side with the database code

Please any suggestions.

Thanks..