How to data into webpage
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
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@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hi David, I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh... 2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com>:
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@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hey, in your view: <select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select> Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com <mailto:untg99@gmail.com>>: 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@gmail.com <mailto:gatorreina@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@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Sorry, it is my mistake, I read as a select option:) But the dropdown menu is working with the very same way! FOREACH, add the entry, and END.
On 21 Aug 2015, at 18:02, Attila Bárdi <attila.bardi@gmail.com> wrote:
Hey,
in your view:
<select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select>
Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com <mailto:gatorreina@gmail.com>> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com <mailto:untg99@gmail.com>>: 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@gmail.com <mailto:gatorreina@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@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hi Atilla, Thank you very much for the reply. I'm afraid that I am trying it but must have something wrong with my syntax. This is how I display raw data. <% FOREACH ID IN MyHash.keys %> <% MyHash.$ID.NAME %> <% END %> But when I do: <select> [% FOREACH ID IN MyHash.Keys %] <option value="[% ID %]" >[% ID %]</option> [% END %] </select> It does not seem to work. Only '% ID %' appears. 2015-08-21 11:02 GMT-05:00 Attila Bárdi <attila.bardi@gmail.com>:
Hey,
in your view:
<select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select>
Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com>:
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@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Sorry, there was a typo, MyHash.keys (I wrote it with capital K).
On 21 Aug 2015, at 18:44, Richard Reina <gatorreina@gmail.com> wrote:
Hi Atilla,
Thank you very much for the reply. I'm afraid that I am trying it but must have something wrong with my syntax.
This is how I display raw data.
<% FOREACH ID IN MyHash.keys %>
<% MyHash.$ID.NAME <http://id.name/> %>
<% END %>
But when I do:
<select> [% FOREACH ID IN MyHash.Keys %] <option value="[% ID %]" >[% ID %]</option> [% END %] </select>
It does not seem to work. Only '% ID %' appears.
2015-08-21 11:02 GMT-05:00 Attila Bárdi <attila.bardi@gmail.com <mailto:attila.bardi@gmail.com>>: Hey,
in your view:
<select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select>
Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com <mailto:gatorreina@gmail.com>> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com <mailto:untg99@gmail.com>>: 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@gmail.com <mailto:gatorreina@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@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm <mailto:dancer-users@dancer.pm> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users <http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Okay, Thanks. Let me know if you notice anything else I did wrong (besides Keys being capitalized) as I still cannot get it to work even after making the k lower case. Thanks for all the help so far. Richard 2015-08-21 11:44 GMT-05:00 Richard Reina <gatorreina@gmail.com>:
Hi Atilla,
Thank you very much for the reply. I'm afraid that I am trying it but must have something wrong with my syntax.
This is how I display raw data.
<% FOREACH ID IN MyHash.keys %>
<% MyHash.$ID.NAME %>
<% END %>
But when I do:
<select> [% FOREACH ID IN MyHash.Keys %] <option value="[% ID %]" >[% ID %]</option> [% END %] </select>
It does not seem to work. Only '% ID %' appears.
2015-08-21 11:02 GMT-05:00 Attila Bárdi <attila.bardi@gmail.com>:
Hey,
in your view:
<select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select>
Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com>:
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@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hooray! Got it to work by changing the brackets from '<' to '[' Thanks for all the help! 2015-08-21 13:16 GMT-05:00 Richard Reina <gatorreina@gmail.com>:
Okay, Thanks. Let me know if you notice anything else I did wrong (besides Keys being capitalized) as I still cannot get it to work even after making the k lower case.
Thanks for all the help so far.
Richard
2015-08-21 11:44 GMT-05:00 Richard Reina <gatorreina@gmail.com>:
Hi Atilla,
Thank you very much for the reply. I'm afraid that I am trying it but must have something wrong with my syntax.
This is how I display raw data.
<% FOREACH ID IN MyHash.keys %>
<% MyHash.$ID.NAME %>
<% END %>
But when I do:
<select> [% FOREACH ID IN MyHash.Keys %] <option value="[% ID %]" >[% ID %]</option> [% END %] </select>
It does not seem to work. Only '% ID %' appears.
2015-08-21 11:02 GMT-05:00 Attila Bárdi <attila.bardi@gmail.com>:
Hey,
in your view:
<select …> [% FOREACH ent IN YourHash.keys %] <option value="[% ent %]" >[% ent %]</option> [% END %] </select>
Dancer + Template Toolkit are pretty easy to use, simple and effective:)
On 21 Aug 2015, at 17:41, Richard Reina <gatorreina@gmail.com> wrote:
Hi David,
I was able to get this code to work so thank you very much as it was very helpful. Now on to try and somehow figure out how I can get the hash refrence into my bootstrap drop down menu. Ughh...
2015-08-19 22:13 GMT-05:00 David H <untg99@gmail.com>:
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@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On 08/20/2015 04:17 AM, Richard Reina 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
The page is generated from a template, just pass your variables containing the data from sql to the template, and the templating engine such as "simple" or "template_toolkit" ( see config.yaml ) will take care about displaying your data. Dancer::Tutorial has pretty good example: https://metacpan.org/pod/Dancer2::Tutorial Also have a look on https://metacpan.org/pod/Dancer2::Plugin::Database
Hey, I splited into three part: 1, the first is the route handler, AJAX backend, ... get '/' => sub { template 'rootTemplate', { } }; ajax '/getAll' => sub { my $allHashRef = HelperModule->fetchAll_hashref(); foreach ( keys %$trac) { push @_, { value => "$_", text => "$trac->{$_}", } } return to_json(\@_); return to_json(\@_); }; 2, data visualization: template toolkit, bootstrap, jquery, ... 3, data gathering in separated Perl module In the separated Perl modul I do all of the queries, updates, creation and returning the object, hashes, arrays, ... to the caller. In this way I can do more efficient testing and a code reusing for example in a Perl script. On Thu, Aug 20, 2015 at 4:17 AM, Richard Reina <gatorreina@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
David, Alex & Atilla, Thank you very much for these well thought out replies. I will try these solutions and I am sure they will be very helpful. Again, thank you for taking the time to write such thorough replies. Richard 2015-08-20 4:56 GMT-05:00 Attila Bárdi <attila.bardi@gmail.com>:
Hey,
I splited into three part:
1, the first is the route handler, AJAX backend, ...
get '/' => sub { template 'rootTemplate', { } };
ajax '/getAll' => sub { my $allHashRef = HelperModule->fetchAll_hashref();
foreach ( keys %$trac) { push @_, { value => "$_", text => "$trac->{$_}", } } return to_json(\@_);
return to_json(\@_); };
2, data visualization: template toolkit, bootstrap, jquery, ... 3, data gathering in separated Perl module
In the separated Perl modul I do all of the queries, updates, creation and returning the object, hashes, arrays, ... to the caller. In this way I can do more efficient testing and a code reusing for example in a Perl script.
On Thu, Aug 20, 2015 at 4:17 AM, Richard Reina <gatorreina@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (4)
-
Alex Mestiashvili -
Attila Bárdi -
David H -
Richard Reina