Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

CGI::Ajax + Template::Toolkit Question

by stonecolddevin (Vicar)
on Mar 07, 2007 at 03:46 UTC ( #603559=perlquestion: print w/ replies, xml ) Need Help??
stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: Got it working. See readmore for code.

#!C:/xampp/perl/bin/perl.exe -w use strict; use CGI; use CGI::Ajax; use Template; # for template toolkit support use CGI::Carp qw[fatalsToBrowser]; #DEBUG ONLY my $config = { INCLUDE_PATH => 'tt/', # or list ref }; my $cgi = CGI->new; my $pjx = CGI::Ajax->new( 'login' => \&do_login ); my $tt = Template->new($config); print $pjx->build_html($cgi, \&main_page); sub do_login { my $input = shift; # do something with $input my $output = $input . " was the input!"; return( $output ); } sub main_page { my $template = 'main.tt'; my $vars = { title =>'AJAX Test' }; my $output = ''; $tt->process($template, $vars, \$output) || die $tt->error(), "\n"; return $output; }

Hey gang.

This is probably something that's right in front of my face, but I'm attempting to get started on some AJAX using CGI::Ajax and Template (to handle HTML processing). The demo from the docs ran great, however my code is throwing this error:

malformed header from script. Bad header=<html><head><s +cript type="text: ajax.cgi

Not only that, but it's returning the HTML as text to the browser.

Here's my code:

#!C:/xampp/perl/bin/perl.exe -w use strict; use CGI; use CGI::Ajax; use Template; # for template toolkit support use CGI::Carp qw[fatalsToBrowser]; #DEBUG ONLY my $config = { INCLUDE_PATH => '/tt', # or list ref INTERPOLATE => 1, # expand "$var" in plain text POST_CHOMP => 1, # cleanup whitespace PRE_PROCESS => 'header', # prefix each template EVAL_PERL => 1, # evaluate Perl code blocks }; my $cgi = CGI->new; my $pjx = CGI::Ajax->new( 'login' => \&do_login ); my $tt = Template->new(); print $pjx->build_html($cgi, \&main_page); sub do_login { my $input = shift; # do something with $input my $output = $input . " was the input!"; return( $output ); } sub main_page { my $template = "main.tt"; return $tt->process($template, { title =>'AJAX Login'}) or die $tt-> +error; }

Any ideas? I'm thinking it's something to do with how build_html in CGI::Ajax tries to cope with process in T::T.

Thanks in advance!

meh.

Comment on CGI::Ajax + Template::Toolkit Question
Select or Download Code
Re: CGI::Ajax + Template::Toolkit Question
by chargrill (Parson) on Mar 07, 2007 at 04:16 UTC

    From the error, it looks like you haven't printed out any HTTP headers. Try print header(); before calling print $pjx->build_html. Also, you might want to actually make use of that $config hashref you've setup there in your call to Template->new( $config ).

    Also, if CGI::Ajax is building some HTML, what's Template going to do for you?

    Unfortunately not using CGI::Ajax, but just T::T (which is where I think you're hitting a snag), the following snippet might help:

    #!/usr/bin/perl use strict; use warnings; use Template; use CGI qw/:standard/; # ... print header(-charset=>'utf-8'); my $vars = { # ... blah blah blah }; my $template = Template->new( INCLUDE_PATH => '/www/path_to_blah/templates/', ); $template->process( q/template.tt/, $vars );


    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: CGI::Ajax + Template::Toolkit Question
by randyk (Parson) on Mar 07, 2007 at 05:12 UTC
    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.

      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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://603559]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2015-07-06 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (84 votes), past polls