diff --git a/lib/Mojolicious/Plugin/TtRenderer/Engine.pm b/lib/Mojolicious/Plugin/TtRenderer/Engine.pm index f30c64a..8035bd4 100644 --- a/lib/Mojolicious/Plugin/TtRenderer/Engine.pm +++ b/lib/Mojolicious/Plugin/TtRenderer/Engine.pm @@ -167,28 +167,25 @@ sub _template_content { return $self->SUPER::_template_content(@_); } + my $data; + my $error = ''; + # Try DATA section - if(defined $options) - { - my $d = $self->renderer->get_data_template($options); - return wantarray ? ($d, '', time) : $d - if $d; - } - else - { - my $loader = Mojo::Loader->new; - foreach my $class (@{ $self->renderer->classes }) - { - my $d = $loader->data($class, $t); - return wantarray ? ($d, '', time) : $d - if $d; - } + if(defined $options) { + $data = $self->renderer->get_data_template($options); + } else { + my $loader = Mojo::Loader->new; + foreach my $class (@{ $self->renderer->classes }) { + $data = $loader->data($class, $t); + last if $data; + } } - my $data = ''; - my $error = "$path: not found"; - my $mod_date = time; - return wantarray ? ($data, $error, $mod_date) : $data; + unless($data) { + $data = ''; + $error = "$path: not found"; + } + return wantarray ? ($data, $error, time) : $data; } 1; diff --git a/t/lite_app.t b/t/lite_app.t index 1bcdf8a..800aa47 100644 --- a/t/lite_app.t +++ b/t/lite_app.t @@ -7,7 +7,7 @@ BEGIN { $ENV{MOJO_MODE}='testing'; }; use utf8; -use Test::More tests => 31; +use Test::More tests => 39; use Mojolicious::Lite; use Mojo::ByteStream 'b'; @@ -26,6 +26,10 @@ get '/with_include' => 'include'; get '/with_wrapper' => 'wrapper'; +get '/badinclude' => 'badinclude'; + +get '/badwrapper' => 'badwrapper'; + get '/with_auto_wrapper' => 'auto_wrapper'; get '/inheritance_base' => 'inheritance_base'; @@ -55,6 +59,12 @@ $t->get_ok('/bar/hello')->content_is("hello"); # With include $t->get_ok('/with_include')->content_is("HelloInclude!Hallo"); +# Bad inclde +$t->get_ok('/badinclude')->status_is(500)->content_like(qr/Exception/i)->content_like(qr/bogus\.inc/); + +# Bad wrapper +$t->get_ok('/badwrapper')->status_is(500)->content_like(qr/Exception/i)->content_like(qr/layouts\/bogus\.html\.tt/); + # With wrapper $t->get_ok('/with_wrapper')->content_is("wrapped"); @@ -102,6 +112,10 @@ Hallo Include! [% INCLUDE 'includes/sub/include.inc' -%] +@@ badinclude.html.tt +[%- INCLUDE 'bogus.inc' -%] +not here + @@ layouts/layout.html.tt w[%- content -%]d @@ -110,6 +124,11 @@ w[%- content -%]d rappe [%- END -%] +@@ badwrapper.html.tt +[%- WRAPPER 'layouts/bogus.html.tt' %-] +not here +[%- END -%] + @@ layouts/auto_layout.html.tt w[%- h.content -%]d diff --git a/t/templates/Foo.pm b/t/templates/Foo.pm new file mode 100644 index 0000000..e22fdde --- /dev/null +++ b/t/templates/Foo.pm @@ -0,0 +1,13 @@ +package + Foo; +1; +__DATA__ + +@@ include.inc +Hello +@@ includes/sub/include.inc +Hallo + +@@ layouts/layout.html.tt +w[%- content -%]d + diff --git a/t/two_data.t b/t/two_data.t new file mode 100644 index 0000000..9113272 --- /dev/null +++ b/t/two_data.t @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +BEGIN { $ENV{MOJO_MODE} = 'testing' }; + +use utf8; + +use Test::More tests => 7; + +use Mojolicious::Lite; +use Test::Mojo; +use File::Temp qw( tempdir ); + +use FindBin (); +use lib "$FindBin::Bin/templates"; + +use_ok 'Foo'; + +push @{app->renderer->classes}, 'Foo'; + +plugin 'tt_renderer' => {template_options => {PRE_CHOMP => 1, POST_CHOMP => 1, TRIM => 1}}; + +app->log->level('fatal'); + +get '/with_include' => 'include'; +get '/with_wrapper' => 'wrapper'; + +my $t = Test::Mojo->new; + +# With include +$t->get_ok('/with_include')->status_is(200)->content_is("HelloInclude!Hallo"); + +# With wrapper +$t->get_ok('/with_wrapper')->status_is(200)->content_is("wrapped"); + +__DATA__ + +@@ wrapper.html.tt +[%- WRAPPER 'layouts/layout.html.tt' -%] +rappe +[%- END -%] + +@@ include.html.tt +[%- INCLUDE 'include.inc' -%] +Include! +[%- INCLUDE 'includes/sub/include.inc' -%]