Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element from array in Template-Toolkit?

[% a = ['one', 'two', 'four'] %]
[% a.1 %] # it prints two. OK!

But when I want this:

[% a = ['one', 'two', 'four'] %]
[% n = 1 %]
[% a.n %] # it doesn't work

How can I use var n in order to get defined element from array?

like image 946
edem Avatar asked Mar 26 '13 00:03

edem


1 Answers

Template Toolkit has same access to list and hash elements - through dot operator. In your code TT thinks that you want to get value in hash a by key 'n'. Solution is to use prefix $ before your actual variable in dot operator, in you case:

[% a = ['one', 'two', 'four'] %]
[% n = 1 %]
[% a.$n %] # now it works
like image 75
alexlopashev Avatar answered Nov 01 '22 21:11

alexlopashev