betacentauri has asked for the wisdom of the Perl Monks concerning the following question:
O Monks,
I have a database with records sorted by a category field. I want to use Template::Toolkit to get a listing with control break by category, i.e. starting from
(1, 2, A, 4) (3, 4, A, 5) (5, 5, B, 2) (6, 2, B, 3) (1, 2, C, 2) (2, 3, C, 2) (1, 3, C, 1)
where A, B, C is the record category, I want to have Template::Toolkit give me
A 1, 2, A, 4 3, 4, A, 5 B 5, 5, B, 2 6, 2, B, 3 C 1, 2, C, 2 2, 3, C, 2 1, 3, C, 1
I have managed to build a somewhat complex data structure like this
[ { cat => 'A', list => [{1, 2, A, 4},{3, 4, A, 5}]}, { cat => 'B', list => [{5, 5, B, 2},{;6, 2, B, 3}]}, { cat => 'C', list => [{1, 2, C, 2},{2, 3, C, 2},{1, 3, C, 1}]} ]
Then my TT template says something like
<% FOREACH catrec IN reclist %> <% catrec.cat %> <% FOREACH rec IN catrec.list %> <% rec %> <% END %> <% END %>
Which works as expected. Now, I feel this should be a task frequent enough so as to guarantee that TT would provide some means to accomplish it directly from the sorted tuples, without resort to the weird transformation I have done. Alas, I have perused the whole TT book by Wardley et al not to find any hint of this.
Is there some easier way to have TT graciously spit what I desire? Also, is "control break" the proper name for what I intend TT to do?
TVMIA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Control break with Template::Toolkit
by choroba (Cardinal) on Nov 13, 2019 at 13:05 UTC | |
by haj (Vicar) on Nov 14, 2019 at 01:02 UTC | |
|
Re: Control break with Template::Toolkit
by haj (Vicar) on Nov 13, 2019 at 13:26 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |