RE: [htmltmpl] Confusion
Brought to you by:
samtregar
From: Philip S T. <phi...@gm...> - 2002-07-29 04:57:14
|
On Sun, 28 Jul 2002, Will wrote: > Suppose from the html I have to parameters coming in: "name"="will" > and "age"="20"... when they are passed to a script running both > CGI.pm and HTML::Template... can I then just use them anyway I want > simply by invoking param("name") or param("age") anywhere in the > script...? Ok, here goes: use CGI; my $cgi = new CGI; my $name = $cgi->param("name"); # Note the $cgi-> before the param. It says that param is a method of # the CGI object named $cgi. use HTML::Template; my $t = new HTML::Template(filename => 'mytemplate.tmpl'); $t->param("name" => "Philip"); # Note the $t-> before param. It says that param is a method of the # HTML::Template object $t. # You can also do this, although there are better ways $t->param("name" => $cgi->param("name")); # which would transfer the CGI parameter name to the HTML::Template # object $t. The better way to do that would be: my $t = new HTML::Template(filename => 'mytemplate.tmpl', associate => $cgi); The only way for you to use the param() method without qualifying it with an object first is if you import it into your program like this: use CGI ':standard'; Then you could just say: my $name = param("name"); # no need of my $cgi = new CGI; $t->param("name" => param("name")); However, I don't particularly advocate importing all the CGI methods into your namespace. For one, it pollutes your namespace, and secondly, it makes things very confusing when you have two methods of the same name, one bound to an object and one not. HTML::Template does not export its methods to your code. You *must* always use them bound to an object. Philip |