[% 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?
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
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