http://qs321.pair.com?node_id=603568


in reply to CGI::Ajax + Template::Toolkit Question

It may be that CGI::Ajax expects build_html to return the html (or updated html) for the page. However, by default, $tt->process($template, $vars) will print the processed output to STDOUT. Try something like the following:
sub main_page { my $output = ''; $tt->process($template, $vars, \$output) || die $tt->error(), "\n"; return $output; }
which will stuff the processed template into $output.

Replies are listed 'Best First'.
Re^2: CGI::Ajax + Template::Toolkit Question
by stonecolddevin (Parson) on Mar 07, 2007 at 07:21 UTC

    Using randyk's solution, i get the following error (from CGI::Ajax):


    Problems

    with the html-generating function sent to CGI::Ajax object


    Current code:

    sub main_page { my $output = ''; my $template = 'main.tt'; $tt->process($template, $config, \$output) || die $tt->error(); return $output; }

    Template code:

    (header.tt)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>[% title %]</title> </head> <body>

    (main.tt)

    <body> <p>Please log in.</p> <p>Username: <input type="text" name="username" id="username" /></p> <p>Password: <input type="password" name="passwd" id="passwd" /></p> <p><input type="submit" name="login" value="login" onClick="login(['us +ername'], ['passwd'], ['authenticated']);"/></p> </body> </html>

    I'm not sure what this error means coming from CGI::Ajax. It's a little unspecific, and I don't see anything in the docs regarding it.

    meh.
      Rather than passing the configuration variables in $config to process, as chargrill noted, you can specify these when creating the $tt object:
      my $config = { INCLUDE_PATH => '/tt', INTERPOLATE => 1, POST_CHOMP => 1, PRE_PROCESS => 'header', EVAL_PERL => 1, }; my $tt = Template->new($config);
      and then specify the variables you want processed in your template in process:
      sub main_page { my $output = ''; my $template = 'main.tt'; my $vars = { title =>'AJAX Login'}; $tt->process($template, $vars, \$output) || die $tt->error(); return $output; }

        UPDATE:Would still like to know if this would be a usable solution, however problem has been solved.

        Ahhh whoops, don't know how I missed that.

        I was turning this over in my head, the show_javascript function returns the javascript as text...would it be better to have a template variable in the <HEAD> section of my template and insert it via T::T? Perhaps that would rid me of the error being thrown by CGI::Ajax.

        Thoughts?

        meh.