in reply to CGI::Application/HTML::Template problem

Hi dhoss!

Taking a cue from jeffa.

Am I right in thinking that by "coerce" you mean using the HTML::Template "associate" option to use a parameter in the CGI query object?

If that is the case then perhaps the following may help.

#!/bin/perl5 use strict; use warnings; use CGI::Carp qw( fatalsToBrowser ); package main; my $webapp = App->new(); $webapp->run(); package App; use base 'CGI::Application'; use CGI; sub cgiapp_get_query { my $self = shift; # initilise the query object # with some params my $q = CGI->new( { 'test' => 'test page', 'greeting' => 'Hi there!', } ); return $q; } sub setup { my $self = shift; $self->start_mode('test'); $self->run_modes([qw|test|]); } sub test{ my $self = shift; my $html; { local $/; $html = <DATA>; } my $tmpl_obj = $self->load_tmpl( \$html, associate => $self->query, ); return $tmpl_obj->output(); } __DATA__ <html> <head> <title><!-- TMPL_VAR NAME = TEST --></title> </head> <body> <p><!-- TMPL_VAR NAME = GREETING --></p> </body> </html>
output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" c_app.pl Content-Type: text/html; charset=ISO-8859-1 <html> <head> <title>test page</title> </head> <body> <p>Hi there!</p> </body> </html> > Terminated with exit code 0.

update:
dhoss! You changed your snippet while I preparing this post :-(
Hope this may still be of some use. :-)

Replies are listed 'Best First'.
Re^2: CGI::Application/HTML::Template problem
by stonecolddevin (Parson) on Jan 12, 2006 at 17:04 UTC
    that works, but i think it's the TMPL_LOOP that's giving me troubles...i think there's something in associating it with CGI (and yes i meant associate by coerce) that the TMPL_LOOP doesn't associate it's tags with CGI...
    meh.
      I found your snippet a bit tricky to digest so I've blown some whitespace into it.

      sub retrieve { my $self = shift; use Data::Dumper; my $q = $self->query; my $tmpl = $self->load_tmpl( "showitem.html", loop_context_vars =>1, associate => $q ); $tmpl->param( product => $self->dbh->selectall_arrayref( q[ SELECT image, price, description, serial FROM product WHERE id = ? ], { Slice => {} }, $q->param('item') ) ); return $frm->build_page( { title => "Boyo's Place: " . $self->dbh->selectrow_array( q[ SELECT name FROM product WHERE id=? ], { Slice => {} }, $q->param('item') ), output => $tmpl->output } ), $q->code(Dumper(\$tmpl)); }
      I would use a lot of temp vars to simplify the code and help establish what is happening.

      e.g.

      my $arrayref = $self->dbh->selectall_arrayref( etc.... die Dumper $arrayref; # on a subsequent run my $arrayref2 = $self->dbh->selectrow_array( etc... die Dumper $arrayref2; # and even die Dumper $q->param('item');
      You need to be sure you are feeding your template loop an array of hash refs. Each hash ref will need

      item => $item_value

      in it. You may have to loop through the dbh output yourself to put that in.

      Hope that helps