First of all, the brackets are unnecessary in list context. If you wish to return a list to pass to the FOREACH directive, then you should return the value of retrieve_all() directly as a list, instead of wrapping it in an array reference and passing that as a list of length one. Nonetheless when I did a test it seemed to work the way you have it.
Second, the WHILE syntax is wrong. What you want is more like this:
[% iter = a.auxdata.scalar %] [% ax = iter.next %] [% WHILE ax %] ID [% ax.id %] [% ax = iter.next %] [% END %]
The reason is two-fold. First of all, in the Template Toolkit, an assignment is not an expression and you can't use it as a test. Second, even if you could, the way you wrote it it would fetch a fresh iterator every time it went through the loop, which would be bad. So you need to fetch the iterator once and then iterate through it with a separate variable.
Third, the default behavior in Template Toolkit is to use list context. You need to use the Template::Stash::Context class to get around this. First you need to initialize your template properly:
my $stash = Template::Stash::Context->new(); my $tt = Template->new({STASH => $stash });
and then you need to call the explicit pseudomethod "scalar" in the template itself, as I showed above.
By the way, if you really do need to support both methods, a better way than using context might be to have a parameter in your "auxdata" method.
In reply to Re: Template Toolkit scalar vs list context
by Errto
in thread Template Toolkit scalar vs list context
by eXile
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |