1 package Reaction::UI::RenderingContext::TT;
4 use aliased 'Reaction::UI::RenderingContext';
5 use aliased 'Template::View';
7 class TT is RenderingContext, which {
10 is => 'ro', required => 1,
11 default => sub { 'Reaction::UI::Renderer::TT::Iter'; },
16 implements 'dispatch' => as {
17 my ($self, $render_tree, $args) = @_;
18 #warn "-- dispatch start\n";
20 my %args_copy = %$args;
21 foreach my $to_render (@$render_tree) {
22 my ($type, @to) = @$to_render;
23 if ($type eq '-layout') {
24 my ($lset, $fname, $next) = @to;
25 local $args_copy{call_next} =
27 ? sub { $self->dispatch($next, $args); }
28 : '' # no point running internal dispatch if nothing -to- dispatch
30 $self->render($lset, $fname, \%args_copy);
31 } elsif ($type eq '-render') {
32 my ($widget, $fname, $over) = @to;
36 local $args_copy{_} = $_[0];
37 $body .= $widget->render($fname, $self, \%args_copy);
40 $body .= $widget->render($fname, $self, \%args_copy);
44 #warn "-- dispatch end, body: ${body}\n-- end body\nbacktrace: ".Carp::longmess()."\n-- end trace\n";
48 implements 'render' => as {
49 my ($self, $lset, $fname, $args) = @_;
51 confess "\$body not in scope" unless defined($body);
53 # foreach non-_ prefixed key in the args
54 # build a subref for this key that passes self so the generator has a
55 # rendering context when [% key %] is evaluated by TT as $val->()
56 # (assuming it's a subref - if not just pass through)
60 my $arg = $args->{$_};
61 ($_ => (ref $arg eq 'CODE' ? sub { $arg->($self, $args) } : $arg))
62 } grep { !/^_/ } keys %$args
65 $body .= $lset->tt_view->include($fname, $tt_args);
66 #warn "rendered ${fname}, body length now ".length($body)."\n";
71 package Reaction::UI::Renderer::TT::Iter;
78 sub pos { shift->{pos} }
81 my ($class, $cr, $rctx) = @_;
82 bless({ rctx => $rctx, cr => $cr, pos => 0 }, $class);
88 my $next = $self->{cr}->();
90 return sub { $next->($self->{rctx}) };
96 while (my $e = $self->next) {
105 foreach my $e (@{$self->all}) {