1 package Reaction::UI::RenderingContext::TT;
4 use aliased 'Reaction::UI::RenderingContext';
5 use aliased 'Template::View';
7 use namespace::clean -except => [ qw(meta) ];
8 extends RenderingContext;
14 my ($self, $render_tree, $args) = @_;
15 #warn "-- dispatch start\n";
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} =
24 ? sub { $self->dispatch($next, $args); }
25 : '' # no point running internal dispatch if nothing -to- dispatch
27 $self->render($lset, $fname, \%args_copy);
28 } elsif ($type eq '-render') {
29 my ($widget, $fname, $over) = @to;
34 local $args_copy{_} = $_[0];
35 local $args_copy{count} = ++$count;
36 $body .= $widget->render($fname, $self, \%args_copy);
39 $body .= $widget->render($fname, $self, \%args_copy);
43 #warn "-- dispatch end, body: ${body}\n-- end body\nbacktrace: ".Carp::longmess()."\n-- end trace\n";
47 my ($self, $lset, $fname, $args) = @_;
49 confess "\$body not in scope" unless defined($body);
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)
58 my $arg = $args->{$_};
59 ($_ => (ref $arg eq 'CODE' ? sub { $arg->($self, $args) } : $arg))
60 } grep { !/^_/ } keys %$args
63 $body .= $lset->tt_view->include($fname, $tt_args);
64 #warn "rendered ${fname}, body length now ".length($body)."\n";
67 __PACKAGE__->meta->make_immutable;