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
    It's possible, but I'm not sure the template is the best place to put logic into:
    #!/usr/bin/perl use warnings; use strict; use Template; my $template = 'Template'->new; my @reclist = ([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]); $template->process(\( join "", <DATA>), {reclist => \@reclist}); __DATA__ [% lastcat = "" %] [% FOREACH catrec IN reclist %] [% IF lastcat != catrec.2 %][% catrec.2 %][% lastcat = catrec.2 %][ +% END %] [% FOREACH rec IN catrec %] [% rec %] [% END %] [% END %]
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      ...but I'm not sure the template is the best place to put logic into:
      MVC enthusiasts may enjoy heated debates about this, but in my opinion those doubts aren't worth their time. I'd easily argue that the desired output is a matter of data presentation and not a matter of logic, so a template is a quite appropriate place. We both have demonstrated that TT is quite capable of doing the transformation :)
Re: Control break with Template::Toolkit
by haj (Vicar) on Nov 13, 2019 at 13:26 UTC
    Well, it's not that difficult to do in a template (but alas, I've done this sort of things many times):
    use 5.020; use warnings; use Template; my $data = [[ qw(1 2 A 4) ], [ qw(3 4 A 5) ], [ qw(5 5 B 2) ], [ qw(6 2 B 3) ], [ qw(1 2 C 2) ], [ qw(2 3 C 2) ], [ qw(1 3 C 1) ], ]; my $template = <<'EOT'; [% SET category = ''; FOREACH line IN data; IF line.2 != category; GET line.2; "\n"; category = line.2; END; GET line.join(" "); "\n"; END; %] EOT my $tt = Template->new; $tt->process(\$template,{data => $data});
    That said, if you're munging your data structure in Perl, then I'd suggest a different structure with a hash as the top level, and lists of lists inside:
    { A => [[qw(1 2 A 4)],[qw(3 4 A 5)]], B => [[qw(5 5 B 2)],[qw(6 2 B 3)]], C => [[qw(1 2 C 2)],[qw(2 3 C 2)],[qw(1 3 C 1)]], }
    ...which can then be processed with a different template. Note the extra sort which is required because hash entries come in random order.
    use 5.020; use warnings; use Template; my $data = [[ qw(1 2 A 4) ], [ qw(3 4 A 5) ], [ qw(5 5 B 2) ], [ qw(6 2 B 3) ], [ qw(1 2 C 2) ], [ qw(2 3 C 2) ], [ qw(1 3 C 1) ], ]; my %munged; for my $record (@$data) { no warnings "uninitialized"; push @{$munged{$record->[2]}}, $record; } my $template = <<'EOT'; [% FOREACH category IN data.keys.sort; GET category; "\n"; FOREACH record IN data.$category; GET record.join(" ");"\n"; END; END; %] EOT my $tt = Template->new; $tt->process(\$template,{data => \%munged});
A reply falls below the community's threshold of quality. You may see it by logging in.