1 package Reaction::UI::RenderingContext::TT;
4 use aliased 'Reaction::UI::RenderingContext';
5 use aliased 'Template::View';
7 class TT is RenderingContext, which {
9 has 'tt_view' => ( is => 'ro', required => 1, isa => View);
12 is => 'ro', required => 1,
13 default => sub { 'Reaction::UI::Renderer::TT::Iter'; },
16 implements 'render' => as {
17 my ($self, $fname, $args) = @_;
19 # foreach non-_ prefixed key in the args
20 # build a subref for this key that passes self so the generator has a
21 # rendering context when [% key %] is evaluated by TT as $val->()
22 # (assuming it's a subref - if not just pass through)
26 my $arg = $args->{$_};
27 ($_ => (ref $arg eq 'CODE' ? sub { $arg->($self) } : $arg))
28 } grep { !/^_/ } keys %$args
31 # if there's an _ key that's our current topic (decalarative syntax
32 # sees $_ as $_{_}) so build an iterator around it.
34 # There's possibly a case for making everything an iterator but I think
35 # any fragment should only have a single multiple arg
37 # we also create a 'pos' shortcut to content.pos for brevity
39 if (my $topic = $args->{_}) {
40 my $iter = $self->iter_class->new(
43 $tt_args->{content} = $iter;
44 $tt_args->{pos} = sub { $iter->pos };
46 $self->tt_view->include($fname, $tt_args);
51 package Reaction::UI::Renderer::TT::Iter;
58 sub pos { shift->{pos} }
61 my ($class, $cr, $rctx) = @_;
62 bless({ rctx => $rctx, cr => $cr, pos => 0 }, $class);
68 my $next = $self->{cr}->();
70 return sub { $next->($self->{rctx}) };
76 while (my $e = $self->next) {
85 foreach my $e (@{$self->all}) {