[% a = ['one', 'two', 'four'] %]
[% a.1 %] # it prints two. OK!但是当我想要这个的时候:
[% a = ['one', 'two', 'four'] %]
[% n = 1 %]
[% a.n %] # it doesn't work如何使用var n从数组中获取已定义的元素?
发布于 2013-03-26 19:40:51
模板工具包有相同的访问列表和散列元素-通过点运算符。在您的代码中,TT认为您希望通过键'n‘获取散列a中的值。解决方案是在点运算符中的实际变量之前使用前缀$,在这种情况下:
[% a = ['one', 'two', 'four'] %]
[% n = 1 %]
[% a.$n %] # now it workshttps://stackoverflow.com/questions/15627199
复制相似问题