Compare commits

...

10 Commits

Author SHA1 Message Date
Graham Ollis
54d13555ef Merge branch 'master' of github.com:abh/mojox-renderer-tt 2012-08-31 08:18:19 -04:00
Graham Ollis
d6d98cc533 fix for win32 2012-08-31 08:18:01 -04:00
Graham Ollis
51af0a7893 return 0 on not found 2012-08-25 07:15:15 -04:00
ollisg
b48b4274db add test case for GH#30 2012-08-24 22:23:37 -04:00
Graham Ollis
e64ce4c484 refactor fetching templates from DATA 2012-08-24 05:47:54 -04:00
Graham Ollis
af61eff483 update email. 2012-08-23 22:27:43 -04:00
Graham Ollis
cf390c5456 update changes 2012-08-23 22:26:09 -04:00
Graham Ollis
20d795cb51 Compatibility with mojo 3.33 2012-08-23 22:20:08 -04:00
Graham Ollis
7455ae1be8 v1.23 2012-08-23 15:27:07 -04:00
Graham Ollis
870d195761 use die instead of render_exception 2012-08-23 15:26:38 -04:00
7 changed files with 164 additions and 30 deletions

12
Changes
View File

@@ -1,6 +1,18 @@
Revision history for Mojolicious::Plugin::TtRenderer
{{$NEXT}}
- Windows compatability with Cwd::abs_path
1.25 August 25, 2012
- Return 0 on not found, die on error (GH#30)
1.24 August 23, 2012
- Compatibility with Mojolicious 3.33+
1.23 August 23, 2012
- Use die instead of render_exception (GH#29)
1.22 August 21, 2012
- Compatibility with Mojolicious 3.05+ (GH#24)
- Avoid deep recursion when exception template dies (GH#25,GH26)
- Fixed test failures (GH#27,GH#28)

View File

@@ -1,12 +1,12 @@
name = Mojolicious-Plugin-TtRenderer
author = Ask Bjørn Hansen <ask@develooper.com>
author = Graham Ollis <plicease@cpan.org>
license = Perl_5
copyright_holder = Ask Bjørn Hansen
# copyright_year = 2009
[Prereqs]
Mojolicious = 2.51
Mojolicious = 3.33
Template = 2.18
[Prereqs / TestRequires ]
@@ -39,7 +39,6 @@ version_regexp = ^release/(.*)
[HasVersionTests]
[MetaTests]
[ReadmeFromPod]
[PodCoverageTests]
[Manifest]
[NextRelease]

View File

@@ -40,9 +40,11 @@ sub _init {
# TODO
# take and process options :-)
my @renderer_paths = $app ? map { abs_path($_) } grep { -d $_ } @{ $app->renderer->paths } : ();
my %config = (
( $app
? (INCLUDE_PATH => (join ":", map { abs_path($_) } @{$app->renderer->paths}))
( @renderer_paths > 0
? (INCLUDE_PATH => join(":", @renderer_paths))
: ()
),
COMPILE_EXT => '.ttc',
@@ -82,27 +84,17 @@ sub _render {
$$output = '';
my @params = ({%{$c->stash}, c => $c, h => $helper}, $output, {binmode => ':utf8'});
$self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c);
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);
return 0 if $provider->not_found;
# Error
unless ($ok) {
state $rendering_exception;
my $e = Mojo::Exception->new($self->tt->error.'');
$$output = '';
$c->app->log->error(qq/Template error in "$t": $e/);
unless($rendering_exception)
{
$rendering_exception = 1;
$c->render_exception($e);
$rendering_exception = 0;
$self->tt->error('');
}
}
die $self->tt->error unless $ok;
return 1;
}
@@ -162,6 +154,8 @@ 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 {1}
@@ -169,23 +163,36 @@ sub _template_content {
my $self = shift;
my ($path) = @_;
my $options = delete $self->{options};
# Convert backslashes to forward slashes to make inline templates work on Windows
$path =~ s/\\/\//g;
my ($t) = ($path =~ m{templates\/(.*)$});
if (-r $path) {
return $self->SUPER::_template_content(@_);
}
my $data;
my $error = '';
# Try DATA section
elsif (my $d = $self->renderer->get_data_template($self->ctx, $t)) {
return wantarray ? ($d, '', time) : $d;
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 }) {
$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;

36
t/default_template.t Normal file
View File

@@ -0,0 +1,36 @@
use strict;
use warnings;
use Test::More tests => 3;
use Test::Mojo;
use Mojolicious::Lite;
# Tell Mojolicious we want to load the TT renderer plugin
app->plugin(
tt_renderer => {
template_options => {
# These options are specific to TT
INCLUDE_PATH => 'templates',
COMPILE_DIR => 'templates_c',
COMPILE_EXT => '.ttc',
# ... anything else to be passed on to TT should go here
},
}
);
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

View File

@@ -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

13
t/templates/Foo.pm Normal file
View File

@@ -0,0 +1,13 @@
package
Foo;
1;
__DATA__
@@ include.inc
Hello
@@ includes/sub/include.inc
Hallo
@@ layouts/layout.html.tt
w[%- content -%]d

48
t/two_data.t Normal file
View File

@@ -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' -%]