Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template-Toolkit and complex variables

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...

like image 911
danwoods Avatar asked Nov 10 '11 23:11

danwoods


2 Answers

Template Toolkit uses a unified syntax for accessing elements of complex structures. This should do what you want:

[% foo.bar.0.id %]
like image 181
friedo Avatar answered Oct 28 '22 01:10

friedo


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 %]
like image 25
singingfish Avatar answered Oct 28 '22 00:10

singingfish