6

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

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
danwoods
  • 4,889
  • 11
  • 62
  • 90

5 Answers5

12

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

[% foo.bar.0.id %]
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
friedo
  • 65,762
  • 16
  • 114
  • 184
6

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 %]
singingfish
  • 3,136
  • 22
  • 25
  • Using the last example displays: "$VAR1 = 1; 1" Which is not quite what I'm looking for. Using the next to last one shows that it's a hash. Using the first example displays nothing. – danwoods Nov 11 '11 at 16:38
  • yep, sorry. Fiddling with complex data structures is a bit of a pain in TT. but the idea is to use the Dumper plugin to work out what it is you have, and act appropriately. – singingfish Nov 12 '11 at 22:45
  • The fact that foo.$x is returning 1 (or true, depending on how you look at it), suggests to me that it might be a clash of VMethod name and hash key problem - see my second answer... – RET Nov 21 '11 at 03:41
  • THANKS! - This worked fantastic for me, old one, but really saved me TONS of time as I was trying to build up a hash and was having "issues". – schu777 Feb 26 '20 at 19:23
2

I appreciate you've used the famous foo and bar methods/keys in your example.

One gotcha in TT is a clash of VMethod name and hash-key. Is it possible that your key name conflicts with one of the built-in TT VMethods? .first, .last, .length or even .keys are candidates for this, and there are probably others.

This can lead to unexpected results, particular when you throw in Automagic Promotion of Scalar to List for Virtual Methods behaviour.

The solution to this is to use the item() VMethod, ie:

[% foo.item("bar").0.id %]
RET
  • 9,100
  • 1
  • 28
  • 33
1

Since $some_hash appears to be blessed from the way you access it in perl, I'd try something like

[% foo.bar.0.id() %]

and see if that works.

flesk
  • 7,439
  • 4
  • 24
  • 33
0

Is it possible you have set a local TT variable called foo somewhere in your existing code? You can get some mysterious results if that happens, along the lines of "I agree that this should work; but it doesn't".

One way to check this is to make your foo explicit, ie:

[% c.stash.foo.bar.0.id %]

If that produces a result, then you've got a namespace problem somewhere.

RET
  • 9,100
  • 1
  • 28
  • 33