Hi Richard,
There are two ways that I get info into a page in Dancer/2.
The first is to use an Ajax call, something like this:
$("#llinktoclick").click(function(event) {
var DoggyName = $('#DoggyName').val();
event.preventDefault(); // Important to ensure the link doesn't get submitted.
$.ajax({
method: "POST",
url: "/getDoggyDetails",
data: { Doggy: DoggyName } // Optional, will be sent through as POST data ( ie. params->{'Doggy'} ),
// you can also use the route, by doing url: "/getDoggyDetails" + '/' + DoggyName , instead
})
.done(function( msg ) {
// Do something here like clear and append a status box.
$('#ResponseText').empty(); // clear
$('#ResponseText').append(msg); // append the return value from your POST call
});
});
Then in your .pm file:
post '/getDoggyDetails' => sub {
my $params = request->params;
my $doggyName = $params->{'Doggy'} || "Not entered"; # the || stops uninitialised value warnings in perl.
return "Please enter a doggy name" if $doggyName eq "Not entered";
# DO stuff here with $doggyName etc...
return "This is the completion message to tell you it is all done";
};
The second way, which is probably what you are more interested in, is to pass it directly from the route. With mysql you can probably do a straight fetchallhashref and then pass the response directly to the page, then iterate through the values with template toolkit.
With your route in the Dancer pm file:
any [ 'get', 'post' ] => '/pagetoLoad' => sub {
my $sql = "SELECT * FROM Doggies";
# mysql connection stuff etc...
# Put it into a hashref
# my $doggieRef = $sth->fetchall_hashref(['ID','DoggyTagID'])); # Double attribute version, not common
my $doggieRef = $sth->fetchall_hashref('ID'); # Normal, single attribute version
# The Ref should contain things like $doggieRef->{'1'}{'Name'} = "Rover"; because of the way we are iterating it through with Template Toolkit.
# I would also recommend Data::Dumper to see the contents of your query
my $dumperDoggieText = Dumper($doggieRef);
print "Doggy text from sql query is: $dumperDoggieText\n";
# And, the other important part, sending it to the page.
template 'templateName.tt', {
'Doggies' => $doggieRef, # Make sure 'Doggies' here is what you will iterate through on the page
'Testing' => "This is text to test to make sure this will come through", # to test, use this like <% Testing %> on your template page
}, {};
};
Then using Template Toolkit, in your template file, you can do something like:
<% FOREACH id IN Doggies.keys %>
<% Doggies.$id.Name %>
<% END %>
And also do our little test just to make sure values are coming through.
<% Testing %>
Which should print out: "This is text to test to make sure this will come through" (without the quotes)
In the end Template Toolkit can be a bit confusing with Hashes but it does support Perl hashes very well and also nested attributes, (hashes of hashes etc...).
I don't have a complete '"working example" on hand but if there is anything confusing just let me know and if anyone else sees anything blatantly wrong, please shout out.
David