Bug 30017

Summary: Should we prefer `loop.first` and `loop.last` over calling `variable.count` to reduce our DB hits
Product: Koha Reporter: Martin Renvoize (ashimema) <martin.renvoize>
Component: TemplatesAssignee: Bugs List <koha-bugs>
Status: NEW --- QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: dcook, jonathan.druart, kyle, nick, tomascohen
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=29859
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23991
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:

Description Martin Renvoize (ashimema) 2022-02-03 15:05:13 UTC
I noticed a pattern recently whereby we often call a count prior to iterating over the resultset:

[% IF things.count %]
<table>
  . . .
  [% FOREACH thing IN things %]
  <td>[% thing.whatever %]</td>
  [% END %]
</table>
[% END %]

This introduces a count(*) DB call. We could instead use the first iteration of the iterator to setup the boilerplate and reduce our DB hits.

[% FOREACH thing IN things %]
[% IF thing.first %]
<table>
[% END %]
  <td>[% thing.whatever %]</td>
[% IF thing.last %]
</table>
[% END %]
[% END %]
Comment 1 Martin Renvoize (ashimema) 2022-02-03 15:06:06 UTC
Doh.. it's wouldn'y be 'thing.first' and 'thing.last'.. it would be the special variables.. 'loop.first' and 'loop.last' in the above example.
Comment 2 David Cook 2022-02-07 02:57:44 UTC
Sounds reasonable to me
Comment 3 Jonathan Druart 2022-02-07 15:48:51 UTC
Sometimes we need to know if there is are least 1 row, to display a title for instance, or actions (check/uncheck all) at the top of the table.

We will then end up with 2 patterns. Not a big deal, but worst noting it.

Maybe better to have a TT var defined at the top of the template then reused all along?