perltemplate-toolkit

Foreach hash with Perl template toolkit


I am using Perl template toolkit for rendering my data. This is the hash that I am passing to the template

'location' => {
               '1' => {
                       'nmi' => 'QB13557343'
                },
               '2' => {
                       'nmi' => 'QB13559843'
                },
             },

and in the template I am looping this hash to get the result but its not displaying The code in the template is:

[% FOREACH loc IN location %]

    <p>NMI: [% loc.nmi %][% location.loc.nmi %]


[% END %]

I tried both loc.nmi and location.loc.nmi but not getting any result. Any help would be greatly appreciated.


Solution

  • It's a hash and not an array. Try to iterate over the keys.

    [% FOREACH key IN location.keys %]
        <p>NMI: [% location.$key.nmi %]</p>
    [% END %]