Catalyst-View-EmbeddedPerl
view release on metacpan or search on metacpan
lib/Catalyst/View/EmbeddedPerl/PerRequest.pm view on Meta::CPAN
return $class->template_from_filesystem($app, $template_extension);
}
sub template_from_filesystem {
my ($class, $app, $template_extension) = @_;
my $template_path = $class->get_path_to_template($app, $template_extension);
open(my $fh, '<', $template_path)
|| die "can't open '$template_path': $@";
local $/; my $slurped = $fh->getline;
close($fh);
return ($slurped, $template_path);
}
sub get_path_to_template {
my ($class, $app, $template_extension) = @_;
my @parts = split("::", $class);
my $filename = (pop @parts);
$filename = String::CamelCase::decamelize($filename);
my $path = "$class.pm";
$path =~s/::/\//g;
my $inc = $INC{$path};
my $base = $inc;
$base =~s/$path$//g;
my $template_path = File::Spec->catfile($base, @parts, $filename);
$template_path .= ".$template_extension" if $template_extension;
$app->log->debug("Looking for template at: $template_path") if $app->debug;
return $template_path;
}
# helpers
sub build_helpers {
my ($class, $init_arg_helpers) = @_;
# Gather helpers from all the places we can
my %helpers = (
$class->default_helpers,
%$init_arg_helpers,
($class->can('helpers') ? $class->helpers() : ())
);
return %helpers;
}
sub default_helpers {
my $class = shift;
return (
view => sub { my ($self, $c, @args) = @_; return $self->view(@args); },
content => sub { my ($self, $c, @args) = @_; return $self->content(@args); },
content_for => sub { my ($self, $c, @args) = @_; return $self->content_for(@args); },
content_append => sub { my ($self, $c, @args) = @_; return $self->content_append(@args); },
content_prepend => sub { my ($self, $c, @args) = @_; return $self->content_prepend(@args); },
content_replace => sub { my ($self, $c, @args) = @_; return $self->content_replace(@args); },
);
}
sub inject_helpers {
my ($class, $sandbox_ns, %helpers) = @_;
foreach my $helper(keys %helpers) {
if($sandbox_ns->can($helper)) {
warn "Skipping injection of helper '$helper'; already exists in namespace $sandbox_ns"
if $ENV{DEBUG_TEMPLATE_EMBEDDED_PERL};
next;
}
eval qq[
package $sandbox_ns;
sub $helper { \$helpers{'$helper'}->(__SELF, __SELF->ctx, \@_) }
]; die "Can't inject helpers: $@" if $@;
}
}
sub render {
my ($self, $c, @args) = @_;
$c->log->debug("Rendering Template: @{[ ref $self ]}") if $c->debug;
my $rendered = eval {
$self->render_template($c, @args);
} || do {
my $error = $@;
$c->log->error("Error processing template: $error");
$c->log->debug($error) if $c->debug;
return $error;
};
$c->log->debug("Template @{[ ref $self ]} successfully rendered") if $c->debug;
return $rendered;
}
sub render_template {
my ($self, $c, @args) = @_;
my $ns = $self->_temple->{sandbox_ns};
no strict 'refs';
no warnings 'redefine';
local *{"${ns}::__SELF"} = sub { $self };
return my $page = $self->_doc->render($self, $c, @args);
}
sub read_attribute_for_html {
my ($self, $attribute) = @_;
return unless defined $attribute;
return my $value = $self->$attribute if $self->can($attribute);
die "No such attribute '$attribute' for view";
}
sub attribute_exists_for_html {
my ($self, $attribute) = @_;
return unless defined $attribute;
return 1 if $self->can($attribute);
return;
}
sub view {
my ($self, $view, @args) = @_;
return $self->ctx->view($view, @args)->get_rendered;
}
__PACKAGE__->meta->make_immutable;
=head1 NAME
( run in 2.492 seconds using v1.01-cache-2.11-cpan-71847e10f99 )