I'm sure this must be answered somewhere on the web but I haven't been
able to find it. In my templates I produce short lists of items from
resultsets. Up to now I've been using code that looks like

 items = some_resultset_rs ;
 IF items.count > 0 ;
   '<ul>' ;
   WHILE (item = items.next) ;
     '<li>' ; item.name ;
   END ;
   '</ul>' ;
 END ;

However, I've noticed that this produces two database queries, one for
the count and one for the while. I'd like to reduce this to a single
database hit. But all my efforts have broken on the legendary TT array
problem. So I've tried:

 items = some_resultset_rs.all ;
 IF items.size > 0;
   '<ul>' ;
   FOREACH item IN items ;
     '<li>' ; item.name ;
   END ;
   '</ul>' ;
 END ;

and I've tried using items.0.defined but all my attempts work when there
are zero items and when there are two or more items, but fail when there
is just one item.

Can anybody point me to the best idiom for this please?

Thanks, Dave

_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to