Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: HTML::Template, CGI, pass template params to next script

by Lori713 (Pilgrim)
on Feb 02, 2004 at 20:24 UTC ( [id://325983]=note: print w/replies, xml ) Need Help??


in reply to Re: HTML::Template, CGI, pass template params to next script
in thread HTML::Template, CGI, pass template params to next script

First, thanks for your responses and explanations. I admit I burst into laughter when I read #6... the idea that I might roll my own when I can barely read and understand any POD... <giggle> I just wish it were even possible to roll my own at this stage! ;-)

I've been trying to play with CGI::Session, but I can't get it to work for me. I don't know if it's because I'm trying to save ALL my CGI form variables PLUS my TMPL_VAR variables... Is that even possible?

As for CGI.pm, wouldn't CGI.pm's "stickyness" be the default behavior? I see where there's a pragma called "-nosticky". I can't seem to pass the variables to the second script, even though I successfully passed them to the first script (and I suspect it's because the variables were in a form and CGI.pm likes that). So, am I correct in my understanding that CGI.pm won't pass the form variables a second time because there's no form?

edited: left the following question out...

Now, on to item #2... that's intriguing. I'd like to make sure I understand what you're saying. I can save all my template variables in an array, then pass that array over to the template and somehow (don't know how yet) I can then "extract" them into individual items I can use? Did I get that right?

Thanks!

Lori

  • Comment on Re: Re: HTML::Template, CGI, pass template params to next script

Replies are listed 'Best First'.
Re:^^ HTML::Template, CGI, pass template params to next script
by jdtoronto (Prior) on Feb 02, 2004 at 21:55 UTC
    I just wish it were even possible to roll my own at this stage! ;-)
    I was jokingly referring to the 'googling' you did when you came up with all sorts of references to getting the $ENV variable and parsing it! Just don't do it please :)
    As for CGI.pm, wouldn't CGI.pm's "stickyness" be the default behavior? I see where there's a pragma called "-nosticky". I can't seem to pass the variables to the second script, even though I successfully passed them to the first script (and I suspect it's because the variables were in a form and CGI.pm likes that). So, am I correct in my understanding that CGI.pm won't pass the form variables a second time because there's no form?
    I am going to pass on comeenting here. Yes, as I recall, CGI.pm's default is sticky, but it is a LONG time since I used it that way.
    I've been trying to play with CGI::Session, but I can't get it to work for me. I don't know if it's because I'm trying to save ALL my CGI form variables PLUS my TMPL_VAR variables... Is that even possible?
    Sheesh! Why does everybody have trouble getting CGI::Session to work! It's easy. OK, here we go:
    1. Create you database table, make sure that you have the required two fields. e.g.
      SET FOREIGN_KEY_CHECKS=0; #---------------------------- # Table structure for sessions #---------------------------- CREATE TABLE `sessions` ( `id` varchar(32) NOT NULL default '', `a_session` text NOT NULL, UNIQUE KEY `id` (`id`) ) TYPE=MyISAM;
      and make sure that the 'a_session' is at least a BLOB size field.
    2. Set up the database and get a DBH object back.
      my $config = { DSN => 'DBI:mysql:session', MySQLunm => 'ipsofalco', MySQLpwd => 'ipsoquango', }; my $dbh = &createDBconnection( $config->{DSN}, $config->{MySQLunm}, $c +onfig->{MySQLpwd} ); # Get a CGI object, it will be filled if a form was #submitted and will have a cookie in teh header #if one had been set. my $q = new CGI; #Initialize the session, if their is a cookie in the #CGI object then the CGISESSID will be the value of an #existing session, otherwise it creates a new session my $session = new CGI::Session("driver:MySQL", $q, {Handle=>$dbh}); #$rdcookie is the value of the cookie that was already # in the header read=past tense in this case. my $rdcookie = $q->cookie("CGISESSID"); #$id is the session id (an MD5 hash) of the session. my $id = $session->id(); # Set cookie with new expire time my $cookie = $q->cookie(-name=>'CGISESSID', -value=>$session->id, -exp +ires=>'+1d');
    3. Now we can write to the session, read from it, clear it and expire things, like this in a crude shopping cart:
      sub addItemCart { my ($cgi, $session, $orderitem) = @_; my $orderitem = &getItemForCart($cgi, $session); # get current cart contents my $cart = $session->param("CART") || []; # add selected item push @{$cart}, $orderitem; # update the CART in the session $session->param( "CART", $cart ); $session->flush(); return displayCart($cgi, $session); }
      Does nothing more than get a hash of an item and adds it to the CART value(a list of hashes) which is saved in the session parameters.
    4. To clear the cart we clear the session value that was saved:
      sub clrCart { my ($cgi, $session) = @_; $session->clear(["CART"]); $session->flush(); return &displayCart($cgi, $session); }
      and in this case display the updated cart.
    Save what you like int he session parameters. Save an entire HASH, darn it, save an array of hashes just like my silly little cart.

    To see some more code - much more complete, have a look at jdtoronto's scratchpad The full working version is posted there.

    jdtoronto

Re: Re: Re: HTML::Template, CGI, pass template params to next script
by jdtoronto (Prior) on Feb 03, 2004 at 00:44 UTC
    I missed part of your questions:
    I can save all my template variables in an array, then pass that array over to the template and somehow (don't know how yet) I can then "extract" them into individual items I can use? Did I get that right?
    Not quite, you can pass it a hash reference, if you want to fill in several TMPL_VAR's you can do this:
    $template->param( 'username' => 'john', 'userstatus' => 'paid' );
    So you can build a HASH and pass in a reference. Oh. lets not forget, in CGI the Vars method will return either a HASH of a HASHREF of all the parameters, instead of iterating over them using the param method you can get then in one bite. If you use
    $hashref = $q->Vars;
    then you get a tied HASHREF which you can pass to H::T.
    $template->param( $hashref );
    will set all the variables for which a hash key exists. In this case <TMPL_VAR name="username"> and <TMPL_VAR name="userstatus">.

    Interestingly, H::T can also accept an object with a param method, similar to CGI. Well, this then means that you should be able to pass the query object to H::T, I havn't tried it, but it should work.

    $template->param( associate => $q );

    HTML::Template is immensely flexible and can be used in many ways, and it can be fed in many and various ways too!

    jdtoronto

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://325983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-18 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found