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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.