memory leaks on CRUD fixed
[catagits/Reaction.git] / lib / Reaction / UI / RenderingContext / TT.pm
CommitLineData
7adfd53f 1package Reaction::UI::RenderingContext::TT;
2
3use Reaction::Class;
4use aliased 'Reaction::UI::RenderingContext';
5use aliased 'Template::View';
6
81393881 7use namespace::clean -except => [ qw(meta) ];
8extends RenderingContext;
7adfd53f 9
f2fef590 10
81393881 11
12our $body;
13sub dispatch {
14 my ($self, $render_tree, $args) = @_;
f2fef590 15#warn "-- dispatch start\n";
81393881 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;
f2fef590 36 $body .= $widget->render($fname, $self, \%args_copy);
81393881 37 });
38 } else {
39 $body .= $widget->render($fname, $self, \%args_copy);
f2fef590 40 }
41 }
81393881 42 }
f2fef590 43#warn "-- dispatch end, body: ${body}\n-- end body\nbacktrace: ".Carp::longmess()."\n-- end trace\n";
81393881 44 return $body;
45};
46sub render {
47 my ($self, $lset, $fname, $args) = @_;
f2fef590 48
81393881 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
7adfd53f 61 };
62
81393881 63 $body .= $lset->tt_view->include($fname, $tt_args);
64#warn "rendered ${fname}, body length now ".length($body)."\n";
7adfd53f 65};
7adfd53f 66
81393881 67__PACKAGE__->meta->make_immutable;
68
69
7adfd53f 701;