Bug 30017 - Should we prefer `loop.first` and `loop.last` over calling `variable.count` to reduce our DB hits
Summary: Should we prefer `loop.first` and `loop.last` over calling `variable.count` t...
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Templates (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-02-03 15:05 UTC by Martin Renvoize
Modified: 2022-02-16 11:19 UTC (History)
5 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Renvoize 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 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?