Gentlemenk,

I wrote a reasonably complicated HTML:Template-based web application that works fine. Now, I want to insert some XMLHttpRequest functionality. I've got the skeleton code working, however, I can't for the life of me figure out how to merge it with HTML::Template. Here is the pseudo-flow of the application --

### in my code my $foo = $cgi->param('foo'); # do a bunch of things with $foo, # get data out of SQLite, # prepare html output, print "Content-Type: text/html\n\n", $template->output; ### in my template <TMPL_LOOP FOO> <TMPL_VAR BAR><br> </TMPL_LOOP>

But, now I've added the XMLHttpRequest code to the template so 'foo' is sent to the server in background, so to say. However, I can't output the entire $template anymore... I have to output only the resulting recordset, receive it back at the client's end, process it and output it using innerHTML to create the same pretty output that I was getting with TMPL_LOOP. The best I can figure is --

### in my code my $foo = $cgi->param('foo'); # figure out if XMLHttpRequest if ($foo) { # do a bunch of things with $foo, # get data out of SQLite, # prepare XMLHttpRequest output print "Content-Type: text/html\n\n", $output; # for all other normal cgi requests } else { # grab cgi params, # do a bunch of things with them, # get data out of SQLite, # prepare html output, print "Content-Type: text/html\n\n", $template->output; } ### in my template, # document.getElementById("output").innerHTML = output; <div id="output"></div>

Well, isn't that terrible? I've just gone and messed up my neat H::T based View and Controller separation. Is there a cleaner, better, more satisfying method?

--

when small people start casting long shadows, it is time to go to bed

In reply to integrating XMLHttpRequest and HTML::Template by punkish

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.