Compare commits

...

4 Commits

Author SHA1 Message Date
Graham Ollis
c00f8934de rm not_found, as it is no longer used. 2013-01-09 14:14:44 -05:00
Graham Ollis
d4902f8bf0 Merge pull request #34 from rkitover/master
fix reporting not found templates
2013-01-09 11:08:03 -08:00
Rafael Kitover
4225658dbc report not finding a template correctly
TtRenderer was dying with a 'not found' error when a template was not
found, instead of returning 0, which was interfering with things like
the error screen.

Use $provider->fetch to find templates and return 0 when a template is
not found.
2013-01-09 13:17:11 -05:00
Rafael Kitover
d45efa5405 failing test for not returning not found correctly 2013-01-09 13:13:53 -05:00
3 changed files with 57 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
Revision history for Mojolicious::Plugin::TtRenderer
{{$NEXT}}
1.35 December 30, 2012
- Documentation fix
1.34 December 29, 2012

View File

@@ -12,6 +12,7 @@ use Mojo::ByteStream 'b';
use Template ();
use Cwd qw/abs_path/;
use Scalar::Util 'weaken';
use POSIX ':errno_h';
__PACKAGE__->attr('tt');
@@ -87,11 +88,26 @@ sub _render {
my $provider = $self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0];
$provider->options($options);
$provider->ctx($c);
$provider->not_found(0);
my $ok = $self->tt->process(defined $inline ? \$inline : $t, @params);
my $ok = do {
if (defined $inline) {
$self->tt->process(\$inline, @params);
}
else {
my @ret = $provider->fetch($t);
return 0 if $provider->not_found;
if (not defined $ret[1]) {
$self->tt->process($ret[0], @params);
}
elsif (not defined $ret[0]) { # not found
return 0;
}
else { # error
return 0 if $! == ENOENT && (not ref $ret[0]); # not found when not blessed exception
die $ret[0];
}
}
};
# Error
die $self->tt->error unless $ok;
@@ -155,7 +171,6 @@ sub new {
sub renderer { @_ > 1 ? $_[0]->{renderer} = $_[1] : $_[0]->{renderer} }
sub ctx { @_ > 1 ? $_[0]->{ctx} = $_[1] : $_[0]->{ctx} }
sub options { @_ > 1 ? $_[0]->{options} = $_[1] : $_[0]->{options} }
sub not_found { @_ > 1 ? $_[0]->{not_found} = $_[1] : $_[0]->{not_found} }
sub _template_modified {
my($self, $template) = @_;
@@ -183,7 +198,6 @@ sub _template_content {
# Try DATA section
if(defined $options) {
$data = $self->renderer->get_data_template($options);
$self->not_found(1) unless defined $data;
} else {
my $loader = Mojo::Loader->new;
foreach my $class (@{ $self->renderer->classes }) {

36
t/default_template2.t Normal file
View File

@@ -0,0 +1,36 @@
use strict;
use warnings;
use Test::More tests => 3;
use Test::Mojo;
use File::Temp qw( tempdir );
use FindBin '$Bin';
use Mojolicious::Lite;
use Mojolicious::Plugin::TtRenderer::Engine ();
my $tt = Mojolicious::Plugin::TtRenderer::Engine->build(
mojo => app,
template_options => {
UNICODE => 1,
ENCODING => 'UTF-8',
INCLUDE_PATH => "$Bin/templates",
}
);
app->renderer->add_handler(tt => $tt);
app->renderer->default_handler('tt');
get '/' => sub {
die 'foo';
};
my $t = Test::Mojo->new;
$t->get_ok('/')
->status_is(500)
->content_like(qr{foo});
__DATA__
@@ index.html.tt
anything