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

Clive Eisen clive at hildebrand.co.uk
Mon Aug 24 11:39:45 EDT 2020


You need to add

template: template_toolkit
engines:
    template:
        template_toolkit:
            start_tag: '<%'
            stop_tag: '%>'

into environments/development.yaml

— 
Clive Eisen
GPG: 3818B5F1






> On 24 Aug 2020, at 16:26, Richard Reina <gatorreina at gmail.com> wrote:
> 
> I am going out of my mind. I have no idea what could be wrong.
> 
> Below is my route (with the hash defined in it), my html and the results I get in the browser.
> 
> get '/results' => sub {
>     
>     # Test the hash
>     my %season= (
> 'TylerMontgomery(2022)' => {
>    'so' => 1,
>    'bb' => 1,
>    'rbis' => 0,
>    'atbats' => 117,
>    'runs' => 2,
>    'hits' => 2
> },
> 'ChaseLangan(2022)' => {
>    'runs' => 4,
>    'hits' => 24,
>    'atbats' => 5,
>    'bb' => 0,
>    'rbis' => 2,
>    'so' => 1
> },
> 'BryceJones(2021)' => {
>    'hits' => 2,
>    'runs' => 2,
>    'atbats' => 4,
>    'bb' => 2,
>    'rbis' => 4,
>    'so' => 1
> },
> );
> 
>     
>     template 'results.tt <http://results.tt/>' => {
> 
> 'title' => 'Get Softball Season Stats',
> 'payload' => \%season,
> 
>     }, {};
> 
> };
> 
> ################################################
> HTML
> ################################################
> <!DOCTYPE html>
> <html lang="en">
>   <head>
>     <!-- head definitions go here -->
>     <meta charset="utf-8">	
>   </head>
>   <body>
>     
>     <% IF payload.size %> <!-- season data -------------------------------------->
>     <% payload.size %>
>     <% USE Dumper; Dumper.dump(payload) %>
>     
>      <div class="container" style="margin-top:20px;">
>       <h3>Trying with Gabor's solution</h3> 
>       <% FOR name IN payload.keys.sort %>
>       <% name %>
>       -------------
>       <%- FOR field IN payload.$name.keys.sort %>
>       <p>Field: </p><% field %> : <% payload.$name.$field -%>
>       <p>Field: </p><% field %> : <% payload.$name.atbats -%>
>       <% END %>
>       <% END %>
>     </div>
>      
>     <div class="container" style="margin-top:20px;">
>       <h3>Trying with Ikegami's solution</h3> 
>       <% FOREACH id IN payload.keys %>
>       <% season = payload.$id %>
>       <p><% season.runs %></p>
>       <p><% season.atbats %></p>
>       <% END %>
>     </div>
>     
>     <br> <br>
>     <div class="container" style="margin-top:20px;">
>       <% USE String %>
>       
>       Name                    atbats so   bb  rbis hits runs
>       <% FOR name IN payload.keys.sort %>
>       <% fname  = String.new(name) -%>
>       <% atbats = String.new(payload.$name.atbats) -%>
>       <% so     = String.new(payload.$name.so) -%>
>       <% bb     = String.new(payload.$name.bb <http://name.bb/> ) -%>
>       <% rbis   = String.new(payload.$name.rbis) -%>
>       <% hits   = String.new(payload.$name.hits) -%>
>       <% runs   = String.new(payload.$name.runs) -%>
>       <% fname.left(22)  %> <% atbats.right(3) -%>    <% so.right(3) -%> <% bb.right(3) -%>  <% rbis.right(3) -%>   <% hits.right(3) -%>  <% runs.right(3) -%>
>       <% END  %>
>       <% END  %>
>     </div>
>     
>     <div class="container" style="margin-top:20px;">
>       <h3>Trying with Perlmonks solution</h3> 
>       <% FOREACH brevet = payload  %>
>       brevet.key <% brevet.key %>
>       brevet.val <% brevet.value %>
>       brevet.$val.hits <% brevet.$value.atbats %>
>       brevet.val.atbats <% brevet.value.runs %>
>       brevet.val.dist <% brevet.value.hits %>
>       <% END %>
>     </div>
>   </body>
> </html>
> 
> #######################################################
> The results that appear in the browser
> #######################################################
> Trying with Gabor's solution
> 
> -------------
> Field:
> 
> :
> Field:
> 
> :
> Trying with Ikegami's solution
> 
> 
> 
> 
> 
> Name atbats so bb rbis hits runs
> Trying with Perlmonks solution
> 
> HASH(0x17982b0) brevet.key brevet.val brevet.$val.hits brevet.val.atbats brevet.val.dist
> Powered by Dancer2 <http://perldancer.org/> 0.300004
> ##############################################################################################
> If I replace 'payload' => \%season, with 'payload' => %season and restart my app with  plackup bin/app.psgi then the results in the browser are:
> TylerMontgomery(2022) TylerMontgomery(2022)
> Trying with Gabor's solution
> 
> TylerMontgomery(2022) ------------- TylerMontgomery(2022)
> Field:
> 
> : TylerMontgomery(2022)
> Field:
> 
> : TylerMontgomery(2022)
> Trying with Ikegami's solution
> 
> TylerMontgomery(2022) TylerMontgomery(2022)
> 
> 
> 
> 
> Name atbats so bb rbis hits runs TylerMontgomery(2022)
> Trying with Perlmonks solution
> 
> TylerMontgomery(2022) brevet.key brevet.val brevet.$val.hits brevet.val.atbats brevet.val.dist
> Powered by Dancer2 <http://perldancer.org/> 0.300004
> 
> 
> 
> 
> El dom., 23 ago. 2020 a las 23:11, Gabor Szabo (<gabor at szabgab.com <mailto:gabor at szabgab.com>>) escribió:
> Try putting that backslash back so it will be
> 
>    template 'results.tt <http://results.tt/>' => {
> 
>         'title' => 'Get Softball Season Stats',
>         'payload' => \%season,
> 
>     }, {};
> Gabor
> 
> On Sun, Aug 23, 2020 at 10:44 PM Richard Reina <gatorreina at gmail.com <mailto:gatorreina at gmail.com>> wrote:
> I was merely passing it as with different var name to distinguish from
> different attempts in my html. Taking out the other case, as I have
> done, has no effect on the results.
> 
> This is what is being passed the html in the paste above:
> 
>    template 'results.tt <http://results.tt/>' => {
> 
>         'title' => 'Get Softball Season Stats',
>         'payload' => %season,
> 
>     }, {};
> 
> And this is the result:
> 
> 
> Trying with payload
> TylerMontgomery(2022) ------------- TylerMontgomery(2022) :
> TylerMontgomery(2022)
> 
> Name atbats so bb rbis hits runs TylerMontgomery(2022)
> 
> 
> 2020-08-23 14:22 GMT-05:00, Gabor Szabo <gabor at szabgab.com <mailto:gabor at szabgab.com>>:
> > 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 <http://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 <mailto:gatorreina at gmail.com>> wrote:
> >
> >>  template 'results.tt <http://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 <mailto: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 <mailto: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 <http://paste.scsys.co.uk/592477>
> >> >>
> 
> _______________________________________________
> 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/20200824/056e7f86/attachment-0001.htm>


More information about the dancer-users mailing list