collection-grid already does Pager and Actions
[catagits/Reaction.git] / lib / Reaction / UI / RenderingContext / TT.pm
1 package Reaction::UI::RenderingContext::TT;
2
3 use Reaction::Class;
4 use aliased 'Reaction::UI::RenderingContext';
5 use aliased 'Template::View';
6
7 use namespace::clean -except => [ qw(meta) ];
8 extends RenderingContext;
9
10
11
12 our $body;
13 sub dispatch {
14   my ($self, $render_tree, $args) = @_;
15 #warn "-- dispatch start\n";
16   local $body = '';
17   my %args_copy = %$args;
18   foreach my $to_render (@$render_tree) {
19     my ($type, @to) = @$to_render;
20     if ($type eq '-layout') {
21       my ($lset, $fname, $next) = @to;
22       local $args_copy{call_next} =
23         (@$next
24           ? sub { $self->dispatch($next, $args); }
25           : '' # no point running internal dispatch if nothing -to- dispatch
26         );
27       $self->render($lset, $fname, \%args_copy);
28     } elsif ($type eq '-render') {
29       my ($widget, $fname, $over) = @to;
30       #warn "@to";
31       if (defined $over) {
32         my $count = 0;
33         $over->each(sub {
34           local $args_copy{_} = $_[0];
35           local $args_copy{count} = ++$count;
36           $body .= $widget->render($fname, $self, \%args_copy);
37         });
38       } else {
39         $body .= $widget->render($fname, $self, \%args_copy);
40       }
41     }
42   }
43 #warn "-- dispatch end, body: ${body}\n-- end body\nbacktrace: ".Carp::longmess()."\n-- end trace\n";
44   return $body;
45 };
46 sub render {
47   my ($self, $lset, $fname, $args) = @_;
48
49   confess "\$body not in scope" unless defined($body);
50
51   # foreach non-_ prefixed key in the args
52   # build a subref for this key that passes self so the generator has a
53   # rendering context when [% key %] is evaluated by TT as $val->()
54   # (assuming it's a subref - if not just pass through)
55
56   my $tt_args = {
57     map {
58       my $arg = $args->{$_};
59       ($_ => (ref $arg eq 'CODE' ? sub { $arg->($self, $args) } : $arg))
60     } grep { !/^_/ } keys %$args
61   };
62
63   $body .= $lset->tt_view->include($fname, $tt_args);
64 #warn "rendered ${fname}, body length now ".length($body)."\n";
65 };
66
67 __PACKAGE__->meta->make_immutable;
68
69
70 1;