Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Template display?

by packetstormer (Monk)
on Jun 20, 2012 at 13:18 UTC ( #977332=perlquestion: print w/ replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I have an output problem within Template. The hash below is being passed to the template file I am simply trying to format it before displaying it. The problem is it seems to being displaying very oddly and I can't figure out why.

# Hash being passed from pl file as %results $VAR1 = { 'Local Studies' => [ { 'closed' => '53' }, { 'open' => '13' } ], 'Local History' => [ { 'open' => '1' }, { 'closed' => '4' } ], 'Reference Dept' => [ { 'open' => '1' }, { 'closed' => '2' } ], 'Admin' => [ { 'open' => '1' } ] };

The template code is below (the idea is a simple table output):

[% USE Dumper %] [% Dumper.dump(results) %] <table border="1"> [% FOREACH key IN results.keys.sort %] <tr> <td>[% key %]</td> <td>[% results.$key.0.open %]</td> <td>[% results.$key.1.closed %]</td> </tr> [% END %] </table>

However the actual output page looks like this:

<tr> <td>Admin</td> <td>1</td> <td></td> </tr> <tr> <td>Local History</td> <td>1</td> <td>4</td> </tr> <tr> <td>Local Studies</td> <td></td> <td></td> </tr> <tr> <td>Reference Dept</td> <td>1</td> <td>2</td> </tr>

Can anyone see why the template is missing the value for the "Local Studies" section? I've been looking at this for a while so I hope it is something silly I am over looking!

Comment on Template display?
Select or Download Code
Re: Template display?
by toolic (Chancellor) on Jun 20, 2012 at 13:26 UTC
    I've never used Template, but my guess is that you need to swap the order of your hashes in the array of 'Local Studies'. It seems you need to have open before closed.
Re: Template display?
by Anonymous Monk on Jun 20, 2012 at 13:26 UTC

    You're iterating over the hash, whose values are arrays, but you're not iterating over the arrays

    You need to iterate over the arrays

      Untested

      [% BLOCK THE_SCOPE_OF_ROPE; FOREACH hash IN results.$key; SET open = hash.open IF hash.open; SET closed = hash.closed IF hash.closed; END; %] <td>[% open | html %]</td> <td>[% closed | html %]</td> [% END %]

      Though you probably want to use  IF hash.exists('open');

Re: Template display?
by dasgar (Deacon) on Jun 20, 2012 at 13:56 UTC

    The 'Local Studies' section has the order of open and closed reversed from the other sections. Your code is trying to access 'Local Studies' then array element 0 and then hash key 'open'. However, your data structure has the hash key 'closed' for array element 0 under 'Local Studies'. Similarly, your code is trying to access 'Local Studies' then array element 0 and then hash key 'closed' instead of hash key 'open'. At least, that's my guess as to what's going on.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-21 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls