Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access elements of an array with Template Toolkit?

I would like to access element of array directly using template toolkit.

e.g. we can easily do this,

[% FOREACH item IN array %]
    [% item %]
[% END %]

Can I access directly 0th , 1st, 2nd etc. elements without using any loop?

e.g.

`[% array[0] %]`
like image 333
mukesh bhoj Avatar asked Dec 06 '25 14:12

mukesh bhoj


1 Answers

You can do it like this:

[% array.0 %]

If the index is inside a variable:

[% array.$index %]

The documentation can be found at www.template-toolkit.org/docs/manual/Variables.html#section_List_References.

like image 58
dgw Avatar answered Dec 08 '25 06:12

dgw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!