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