Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Dancer2 - How to loop over a ref to a HoH

by fishmonger (Pilgrim)
on Jun 10, 2014 at 03:26 UTC ( #1089380=perlquestion: print w/ replies, xml ) Need Help??
fishmonger has asked for the wisdom of the Perl Monks concerning the following question:

I'm just beginning to learn how to use the Dancer2 framework and am having problems displaying the results of a $sth->fetchall_hashref DBI querry.

I started with the example code from the "Our first route handler" section in the Dancer2::Tutorial documentation and adjusted it slightly to match my requirements.

Route Handler:

get '/partners' => sub { my $dbh = get_db_connection(); my $sth = $dbh->prepare("SELECT TPID, TPCOMPNAME, TPTYPE FROM TRADINGPARTNERPROFILE ORDER BY TPID LIMIT 2"); $sth->execute; my $partners = $sth->fetchall_hashref('TPID'); my $dump = Dumper($partners); $dbh->disconnect; template 'show_partners.tt', { 'dumper' => $dump, 'partners' => $partners, }; };\

Template:

<div> <pre><% dumper %></pre> <% FOREACH partner IN partners.keys %> Partner: <% partners.$partner.TPCOMPNAME %> <% partners.$partner.T +PTYPE %><br> <% END %> </div>

Resulting html output

<div> <pre>$VAR1 = { 'AB DISTRIBUTING INC.' => { 'TPTYPE' => '1', 'TPID' => 'AB DISTRIBUTING INC.' +, 'TPCOMPNAME' => 'AB Distributing + Inc.' }, 'A.T. CROSS ' => { 'TPTYPE' => '1', 'TPID' => 'A.T. CROSS ', 'TPCOMPNAME' => 'A.T. Cross ' } }; </pre> Partner: <br> </div>

What am I doing wrong which is preventing the desired output in the loop?

Edit:
One factor which may be important is that I'm testing this on Win7 perl v5.16.1 and will be deploying the app on a CentOS system which will probably be running perl v5.18.2

Comment on Dancer2 - How to loop over a ref to a HoH
Select or Download Code
Re: Dancer2 - How to loop over a ref to a HoH (template engine?)
by Anonymous Monk on Jun 10, 2014 at 07:23 UTC
    What template engine?

      I was using Dancer's "simple" engine, which clearly doesn't parse the template vars the same as TT. I guess I was a little tired and not thinking clearly, because that fact should have been obvious.

      Once I changed the engine to "Template_Toolkit" in config.yml and corrected the syntax in the template files it worked correctly.

      Thanks

Re: Dancer2 - How to loop over a ref to a HoH
by arkturuz (Curate) on Jun 10, 2014 at 08:54 UTC
        Sorry about that, somehow I missed it, and it's been a long time since I last hacked on Template Toolkit.

        Could this solve your issue?

        <% FOREACH partner IN partners.values %> Partner: <% partner.TPCOMPNAME %> <% partner.TPTYPE %><br> <% END %>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1089380]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-12-22 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (113 votes), past polls