title: Template Toolkit, test for last iteration in a nested loop
body: <p>I'm using template toolkit to form a simple JSON response (see the code below).
  I need to put a comma after all elements of the response except the last.</p>
  
  <p>I believe I need to make use of TTs iterator, however I'm not getting it right.<br>
  With this code, a comma is still printed on the end of the last element. </p>
  
  <p>The problem lies with the section that contains</p>
  
  <pre><code>[% UNLESS outer.last &amp;&amp; loop.last %],[% END %]
  </code></pre>
  
  <p>this should add a comma unless the outer and inner loops are on their last iteration.</p>
  
  <p>Any help on what I'm getting wrong greatly appreciated.</p>
  
  <pre><code>{ "success": true, "filesdata": [
  [%~ USE outer = iterator(objects); FOREACH object IN outer;
      FOREACH rep IN object.reps;
          IF rep.rep == reptype %]
  { "id":"[% object.id | xml %]", "url":"[% rep.src | xml %]", "story":"[% object.story | xml %]" }[% UNLESS outer.last &amp;&amp; loop.last %],[% END %]
          [%~ END;
      END;
  END ~%]
  ] }
  </code></pre>