Template-Toolkit and complex variables
Asked Answered
I

5

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

Instrumentation answered 10/11, 2011 at 23:25 Comment(0)
M
12

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

[% foo.bar.0.id %]
Monotonous answered 10/11, 2011 at 23:34 Comment(0)
W
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 %]
Whitebook answered 10/11, 2011 at 23:56 Comment(4)
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.Instrumentation
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.Whitebook
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...Nambypamby
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".Trickery
N
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 %]
Nambypamby answered 21/11, 2011 at 3:37 Comment(0)
C
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.

Chestonchest answered 14/11, 2011 at 19:33 Comment(0)
N
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.

Nambypamby answered 21/11, 2011 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.