Say I'm working in Perl using Catalyst. I assign an array of hashes to a a variable, ie:
my @array = ($some_hash);
$c->stash->{foo}->{bar} = \@array;
How do I select an element from $some_hash, such as 'id', in Template Toolkit? In Perl I can access it via $c->stash->{foo}->{bar}->[0]->id
...
All help is greatly appreciated, and I'm a bit of a Perl newb, so if anything looks out of place, please let me know. Thanks in advance...
Template Toolkit uses a unified syntax for accessing elements of complex structures. This should do what you want:
[% foo.bar.0.id %]
The following kind of thing is helpful when you want to work out what's going on in complex data structures in TT:
[% USE Dumper; Dumper.dump_html(foo) %]
.. see what kind of data TT thinks you have:
[% foo %]
... or further down the rabbit warren:
[% FOREACH x IN foo.keys;
USE Dumper; Dumper.dump_html(foo.$x);
foo.$x ; # to see what kind of ref it is
END %]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With