[dancer-users] How to data into webpage
Attila Bárdi
attila.bardi at gmail.com
Fri Aug 21 18:29:17 BST 2015
Sorry, there was a typo, MyHash.keys (I wrote it with capital K).
> On 21 Aug 2015, at 18:44, Richard Reina <gatorreina at 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 at gmail.com <mailto:attila.bardi at 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 at gmail.com <mailto:gatorreina at 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 at gmail.com <mailto:untg99 at 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 at gmail.com <mailto: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 <mailto:dancer-users at 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 at dancer.pm <mailto:dancer-users at 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 at dancer.pm <mailto:dancer-users at 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 at dancer.pm <mailto:dancer-users at 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 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/20150821/aaad788a/attachment.html>
More information about the dancer-users
mailing list