[dancer-users] How tod displaye aMulti Dimesional Hash in Template Toolkit

Gabor Szabo gabor at szabgab.com
Sun Aug 23 15:22:45 EDT 2020


OK, so I don't understand why do you pass the same %payload 3 times, but
you need to pass references in all 3 cases.

template 'results.tt' => {
'title' => 'Get Softball Season Stats',
'games' => \%season,
'season' => \%season,
'payload' => \%season,
'F_NAME' => 'Geraldo',


    }, {};

On Sun, Aug 23, 2020 at 9:38 PM Richard Reina <gatorreina at gmail.com> wrote:

>  template 'results.tt' => {
>
>         'title' => 'Get Softball Season Stats',
>         'payload' => \%season,
>
>     }, {};
>
> Results in this:
>
> Trying with payload
> ------------- :
>
> Name atbats so bb rbis hits runs
>
>
>
> 2020-08-23 12:53 GMT-05:00, Gabor Szabo <gabor at szabgab.com>:
> > I think you should be passing references:
> >
> > 'payload' => \%season,
> >
> > See the backslash.
> >
> > Gabor
> >
> >
> >
> > On Sun, Aug 23, 2020 at 8:45 PM Richard Reina <gatorreina at gmail.com>
> wrote:
> >
> >> Ok, maybe I've overlooked something but here is what I get with very
> >> simple call to a route and using very simple html.
> >>
> >> http://paste.scsys.co.uk/592477
> >>
> >> 2020-08-23 9:57 GMT-05:00, Gabor Szabo <gabor at szabgab.com>:
> >> > Have you checked the content when you "view source" in the browser?
> >> > I had cases when my code did not show up because I had some HTML
> >> > issues.
> >> > I wasted so much time till I figured out the content is there just
> >> hidden.
> >> >
> >> > My example is stand-alone using Template::Toolkit,
> >> > when using Dancer you don't need that, you only need to enable it in
> >> > the
> >> > config file
> >> > and you pass your data to the "template" function.
> >> >
> >> > The TT part is almost identical though I think in Dancer we use <% for
> >> tags
> >> > and not
> >> > the default [% as in my example.
> >> >
> >> > Gabor
> >> >
> >> > On Sun, Aug 23, 2020 at 5:46 PM Richard Reina <gatorreina at gmail.com>
> >> wrote:
> >> >
> >> >> Hi Gabor,
> >> >>
> >> >> Thank you very much for your help. I appreciate your answer and all
> >> >> the wonderful tutorials at perlmaven, but so far I am unable to get
> >> >> this answer to work for me. My my hash %season seems to look the same
> >> >> as your %payload but when I try your solution only the name of the
> >> >> player is displayed. I do not use 'use Template'; in my backend code
> >> >> and am not really sure what the exact role of create.pl, is it for
> >> >> teaching purposes or is there a step within it that I must apply to
> >> >> get my %season to look like your %payload before I return it to my
> >> >> Dancer app? I will continue to experiment with your solution and try
> >> >> to find a place where I can post exactly what I am getting as a
> >> >> result.
> >> >>
> >> >> Thanks again,
> >> >>
> >> >> Richard
> >> >>
> >> >> 2020-08-23 7:18 GMT-05:00, Gabor Szabo <gabor at szabgab.com>:
> >> >> > I am not sure if this is what you meant, but using your data I
> wrote
> >> >> > two
> >> >> > examples here: https://perlmaven.com/template-toolkit-hash-of-hash
> >> >> >
> >> >> > Gabor
> >> >> >
> >> >> > On Sun, Aug 23, 2020 at 12:33 AM Richard Reina
> >> >> > <gatorreina at gmail.com>
> >> >> > wrote:
> >> >> >
> >> >> >> I have the following perl multidimensional hash that is passed
> from
> >> >> >> Dancer to Template Toolkit as a hash reference. I am having
> trouble
> >> >> >> figuring out how to display it in Template Toolkit.
> >> >> >>
> >> >> >> $VAR1 = {
> >> >> >>           'TylerMontgomery(2022)' => {
> >> >> >>                                    'so' => 1,
> >> >> >>                                    'bb' => 1,
> >> >> >>                                    'rbis' => 0,
> >> >> >>                                    'atbats' => 7,
> >> >> >>                                    'runs' => 2,
> >> >> >>                                    'hits' => 2
> >> >> >>                                  },
> >> >> >>           'ChaseLangan(2022)' => {
> >> >> >>                                      'runs' => 4,
> >> >> >>                                      'hits' => 4,
> >> >> >>                                      'atbats' => 5,
> >> >> >>                                      'bb' => 0,
> >> >> >>                                      'rbis' => 2,
> >> >> >>                                      'so' => 1
> >> >> >>                                    },
> >> >> >>           'BryceJones(2021)' => {
> >> >> >>                             'hits' => 2,
> >> >> >>                             'runs' => 2,
> >> >> >>                             'atbats' => 4,
> >> >> >>                             'bb' => 1,
> >> >> >>                             'rbis' => 4,
> >> >> >>                             'so' => 1
> >> >> >>                           },
> >> >> >> };
> >> >> >>
> >> >> >> This is how I iterate it in perl.
> >> >> >>
> >> >> >> foreach my $name (sort keys %season) {
> >> >> >>         printf "%-27.27s", "$name: ";
> >> >> >>         foreach my $stat (sort keys %{ $season{$name} }) {
> >> >> >>             printf "%-12.12s", "$stat: $season{$name}{$stat} ";
> >> >> >>          ## cal. avg
> >> >> >>  $season{$name}{AVG} = $season{$name}{hits} /
> >> >> >> $season{$name}{atbats};
> >> >> >>         }
> >> >> >>
> >> >> >>
> >> >> >>     printf "%4s %.3f\n", "avg:", $season{$name}{AVG};
> >> >> >> }
> >> >> >>
> >> >> >> I then pass it back to my dancer app as a ref. with return
> >> >> >> \%season;
> >> >> >> and then to template toolkit with:
> >> >> >>
> >> >> >>   template 'softball.tt' => {
> >> >> >>
> >> >> >>         'title' => 'Get Softball Season Stats',
> >> >> >>         'Season' => $season,
> >> >> >>
> >> >> >>
> >> >> >>     }, {};
> >> >> >>
> >> >> >> But I am lost trying to get it to display with template toolkit.
> >> >> >> Here's what I've tried so far.
> >> >> >>
> >> >> >> <table style="width:100%; line-height:40px;">
> >> >> >>         <% FOREACH Season = Season %>
> >> >> >>         <tr>
> >> >> >>           <td width="5">Season.key <% Season.key %></td>
> >> >> >>           <td width="5">Season.val <% Season.value %></td>
> >> >> >>           <td width="5">Season.val.atbats <% Season.value.atbats
> %>
> >> >> >>           <td width="5">Season.val.hits <% Season.value.hits %>
> >> >> >>         </tr>
> >> >> >>         <% END %>
> >> >> >>
> >> >> >> Thanks for any help.
> >> >> >> _______________________________________________
> >> >> >> dancer-users mailing list
> >> >> >> dancer-users at lists.preshweb.co.uk
> >> >> >> https://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/20200823/fc679a29/attachment-0002.htm>


More information about the dancer-users mailing list