| 1 | package Catalyst::View::Template::Declare; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw(Catalyst::View::Templated); | ||
| 5 | use Class::C3; | ||
| 6 | require Module::Pluggable::Object; | ||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | |||
| 10 | sub COMPONENT { | ||
| 11 | my $self = shift; | ||
| 12 | my $c = shift; | ||
| 13 | my $class = ref $self || $self; | ||
| 14 | |||
| 15 | # find sub-templates | ||
| 16 | my $mpo = Module::Pluggable::Object->new(require => 0, | ||
| 17 | search_path => $class, | ||
| 18 | ); | ||
| 19 | |||
| 20 | # load sub-templates (and do a bit of magic niceness) | ||
| 21 | my @extras = $mpo->plugins; | ||
| 22 | foreach my $extra (@extras) { | ||
| 23 | $c->log->info("Loading subtemplate $extra"); | ||
| 24 | |||
| 25 | # load module | ||
| 26 | if (!eval "require $extra"){ | ||
| 27 | die "Couldn't include $extra: $@"; | ||
| 28 | } | ||
| 29 | |||
| 30 | # make the templates a subclass of TD (required by TD) | ||
| 31 | { | ||
| 32 | no strict 'refs'; | ||
| 33 | push @{$extra. "::ISA"}, 'Template::Declare'; | ||
| 34 | } | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | # init Template::Declare | ||
| 39 | Template::Declare->init(roots => [$class, @extras]); | ||
| 40 | |||
| 41 | # init superclasses | ||
| 42 | $self->next::method($c, @_); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _render { | ||
| 46 | my ($self, $template) = (shift, shift); | ||
| 47 | |||
| 48 | Template::Declare->new_buffer_frame; | ||
| 49 | local *_ = $_[0]; | ||
| 50 | my $out = Template::Declare->show($template, $self->context, @_); | ||
| 51 | Template::Declare->end_buffer_frame; | ||
| 52 | |||
| 53 | $out =~ s/^\n+//g; # kill leading newlines | ||
| 54 | return $out; | ||
| 55 | } | ||
| 56 | |||
| 57 | package c; | ||
| 58 | use PadWalker qw(peek_my); | ||
| 59 | our $AUTOLOAD; | ||
| 60 | sub AUTOLOAD { | ||
| 61 | shift; # kill class | ||
| 62 | |||
| 63 | # walk up the stack looking for the Catalyst context | ||
| 64 | # in a lexical somewhere (evil, yes.) | ||
| 65 | my $frames_up = 1; | ||
| 66 | my $context; | ||
| 67 | while($frames_up < 300 && !$context){ | ||
| 68 | ($context) = | ||
| 69 | map { $$_ } | ||
| 70 | grep {eval{$$_->isa('Catalyst')}} | ||
| 71 | values %{peek_my($frames_up++)}; | ||
| 72 | } | ||
| 73 | die "INTERNAL ERROR: No Catalyst context found!" if !$context; | ||
| 74 | |||
| 75 | $AUTOLOAD =~ s/^c:://; # kill package c | ||
| 76 | return $context->$AUTOLOAD(@_); | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | __END__ |