[Templates] Templates and AJAX
Larry Leszczynski
larryl@emailplus.org
Sun, 19 Nov 2006 12:01:57 -0700 (Mountain Standard Time)
On Sun, 19 Nov 2006, Buddy Burden wrote:
> We use OpenThought (http://www.openthought.net/) with TT2.
[snip]
> I understand that CGI::Application works well with TT2, but I don't know
> how difficult it would be to convince the OT code to do that.
I have an app built with TT2 and CGI::Application, and found it _very_
easy to plug in AJAX functionality using OpenThought.
For example I have a login page where you can type in your username and
request your password hint, something like this (stripped down):
<!-- this is where hint gets pasted in -->
<p id="password_hint"> </p>
<form method="POST" action="login">
Username: <input type="text" name="username" /><br />
<a href="#" onclick="showHint();return false">Show Hint</a>
</form>
<script>
// Pass the current value of "username" form field
// to the CGI::Application runmode "show_pw_hint":
function showHint {
OpenThought.CallUrl('[% self_url %]/show_pw_hint', 'username');
}
</script>
Then my CGI::Application runmode looks like this (error handling stripped
out):
sub show_pw_hint
{
my $self = shift;
my $username = $self->query->param('username');
my $hint = ... DB lookup of hint for $username ... ;
# Replace content of "password_hint" HTML element,
# and set focus to "username" form field:
return $self->_do_OT({
html => {password_hint => "Your hint is: $hint"},
focus => 'username',
error => $any_error_msg,
#fields => {},
#javascript => '',
});
}
# Helper function to do OpenThought stuff:
#
sub _do_OT
{
my ($self, $args) = @_;
my $error = delete $args->{error};
my $OT = OpenThought2->new();
if ($error) {
my $url = $self->param('self_url') .
'/error_runmode?msg=' . uri_escape($error);
return $OT->parse_and_output({url => $url});
}
else {
return $OT->parse_and_output($args);
}
}
Larry