[dancer-users] How to data into webpage

David H untg99 at gmail.com
Thu Aug 20 04:13:50 BST 2015


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


On Thu, Aug 20, 2015 at 11:47 AM, Richard Reina <gatorreina at gmail.com>
wrote:

> Have been learning Dancer2 over the last few weeks and really like it.
> Wondering if someone can tell me how I can load data from a MySQL database
> into a dancer rendered page. I am comfortable writing SQL in perl via
> perl->DBI just don't have any idea how to bring the data from a fetch into
> a webpage so that I can display it directly on the page or load it into a
> dropdown menu. Any help is greatly appreciated.
>
> Thanks
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150820/ce97bc85/attachment.html>


More information about the dancer-users mailing list