mikeraz has asked for the wisdom of the Perl Monks concerning the following question:

Having applied the methods at Debugging Template Toolkit I see that I'm passing my TT template:

DUMP: $VAR1 = { 'blist' => { 'Central600' => { 'checked' => 'on', 'curr' => 'off', 'desc' => 'Oregon 600-XTR, May 30-31 ' }, 'TriCities200' => { 'checked' => 'no', 'curr' => 'off', 'desc' => 'Tri-Cities 200, April 18 ' }, 'CoveredBridges400' => { 'checked' => 'on', 'curr' => 'yes', 'desc' => 'Covered Bridges 400, May 9' }, }, # end of %blist # other items }

And in my template file this:

Brevets spec'd manually [% blist.CoveredBridges400.desc %] [% blist.CoveredBridges400.curr %] [% blist.CoveredBridges400.checked %] end Brevets spec'd manually

yields the expected:

Brevets spec'd manually Covered Bridges 400, May 9 off on end Brevets spec'd manually

Yet when I try to access the hash values in some sort of FOREACH construct my lack of TT understanding mocks me by not yeilding up the desc, curr and checked values for each of the hashes in blist.

(This is the current of many iterations in trying to figure out what I don't yet know)

[% FOREACH bb IN blist %] 4 bb $bb <br/> 5 bb.key $bb.key <br/> 6 blist.bb blist.$bb <br/> 7 bb.item desc $bb.key.item('desc')<br/> 7 blist.bb.keys blist.$bb.keys<br/> [% FOREACH bkey = blist.$bb.keys.shift %] [% bkey %] => [% # blist.$bb.$bkey %] [% END %] 7a blist.bb.vals blist.$bb.values<br/> [% FOREACH ba = blist.$bb.values.list %] [% ba %] [% END %] [% FOREACH ba IN blist.bb.values %] $ba [% END %] 8 [% blist.$bb.desc %] blist.($bb.key).desc or blist.bb.desc +<br/> 9 [% blist.$bb.curr %] blist.($bb.key).curr or blist.bb.curr +<br/> 10 [% blist.$bb.checked %] [% blist.$bb.key.checked %] <br/> [% END %]
providing:
4 bb HASH(0xd977f0) 5 bb.key CoveredBridges400 6 blist.bb blist.HASH(0xd977f0) 7 bb.item desc CoveredBridges400('desc') 7 blist.bb.keys blist.ARRAY(0xd8f730) 7a blist.bb.vals blist.ARRAY(0xd8f7b0) 8 blist.(CoveredBridges400).desc or blist.bb.desc 9 blist.(CoveredBridges400).curr or blist.bb.curr 10

In any case I'm not grokking how to walk trough the hash of hashes and extract the values.

So Monks, please take up your favorite cluesticks and beat the rocks that cover my understanding.

Be Appropriate && Follow Your Curiosity

Replies are listed 'Best First'.
Re: Hash of Hash Access in Template Toolkit
by mikeraz (Friar) on Apr 12, 2009 at 02:31 UTC

    Ah the joy of posting a question and then figuring out the answer on your own. Is it the mind clarifying effect of posing the question? Does the revelation come from putting your trust in a higher power? Whateva.

    Here is the code that does what I was attempting. Diffing with the non-working example is left as an exercise for the reader.

    [% FOREACH bb IN blist.keys %] [% bb %] => [% blist.$bb %] <br/> [% FOREACH bkey IN blist.$bb.keys %] [% bkey %] => [% blist.$bb.$bkey %] [% END %] can I just specify? [% blist.$bb.curr %] <br/> [% END %]

    Yields:

    CoveredBridges400 => HASH(0xade6b0) checked => on curr => yes desc => Covered Bridges 400, May 9 can I just specify? off
    Be Appropriate && Follow Your Curiosity
      Is it the mind clarifying effect of posing the question?

      Absolutely. In your case, at least ;-)

      That happens often, and in most of these cases we don't even see the question here. Thanks for sharing the answer.