Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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.$ %]</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!

Replies are listed 'Best First'.
Re: Template display?
by dasgar (Curate) 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.

Re: Template display?
by toolic (Bishop) 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


      [% BLOCK THE_SCOPE_OF_ROPE; FOREACH hash IN results.$key; SET open = IF; 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');

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2016-06-27 11:20 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (337 votes). Check out past polls.