Hi Monks!
I am trying to use HTML::Template to process the data results returned from my SQL query instead of returning json to the jQuery to process and display it. I can only get it to work if I return json, it ignores my HTML::Template tags in the .tmpl file, is there a way not to send back json and let the template handling it?
I have sample code to show what I am trying to explain: search.tmpl
This is the search form using jQuery autocomplete:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Search</title> <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript"> var debug = false; $(document).ready(function() { $( "#search" ).autocomplete({ source: function(request, response) { $.ajax({ url: "search.pl", dataType: "json", data: { search: request.term, }, success: function(data) { // return data stuff here }, error: function(response){ if (debug == true) {console.log(response)}; alert('Somethings broken in the AJAX return'); } }); }, minLength: 5, delay: 500 }); }); </script> </head> <body> <div id="container"> <h2>TEST</h2> <div id="namesearch"> <form id="form_search" method="get" name="form_search"/> <label>Search: </label> <input type="text" name="search" id="search" class="search_box"/> </form> </div> <div> <p id="submitted"></p> <!-- display resutls from search here --> <div id='RESULTS'></div> <!-- end displaying results --> <!-- All Data - Here is where I want the data to display--> <TMPL_LOOP NAME=ALL_DATA> <TMPL_VAR NAME=NAME> <TMPL_VAR NAME=CITY> <TMPL_VAR NAME=STATE> </TMPL_LOOP> <TMPL_VAR NAME=COUNT> <!-- Data Ends --> </div> </body> </html>

This is the Perl code that will process the request from the jQuery on the .tmpl
Search.pl

... my $main_tmpl = HTML::Template->new(filename => 'search.tmpl', die_on_ +bad_params => 1); ... sub data { my $id = shift || return; my $sql = qq{SELECT distinct name, city, state FROM my_table WHERE name like ? order by name asc}; my $sth = $dbh->prepare($sql); $sth->execute(${id}.'%') or die "SQL Error: $DBI::errstr\n"; my $all_data = $sth->fetchall_arrayref({}); my @loop_data = (); # initialize an array to hold loop # Count things my $c = 0; my @count; foreach my $row (@{$all_data}) { $c++; my %row_data; # get a fresh hash for the row data # fill in these rows $row_data{NAME} = $row->{NAME} || ''; $row_data{CITY} = $row->{CITY} || ''; $row_data{STATE} = $row->{STATE} || ''; push @count, $c; # the crucial step - push a reference to this row into the loop push(@loop_data, \%row_data); } # Count things my $counter = @count; # I need to load the data here but it doesn't. # load values into the template $main_tmpl->param( ALL_DATA => \@loop_data, COUNT => $counter, ); # If sending data through json use this # This works if I use jQuery to process the returned json from here # but I am trying to load the data using HTML::Template =code print $q->header(-type => "application/json"); if(@loop_data){ print to_json(\@loop_data); exit; }else{ my $json_string = [{ result => "No data"}]; print to_json($json_string); exit; } =cut } # end sub

Thanks for looking!

In reply to HTML::Template data loading by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.