Skip to content

Commit

Permalink
Change to use relative paths, add exception template for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusramberg committed Feb 6, 2011
1 parent fc453fe commit 323d5a9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Revision history for MojoX-Renderer-TT

- Change to use relative paths.

{{$NEXT}}

1.0 January 22, 2011
Expand Down
15 changes: 5 additions & 10 deletions lib/MojoX/Renderer/TT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use base 'Mojo::Base';
use File::Spec ();
use Mojo::ByteStream 'b';
use Template ();
use Cwd qw/abs_path/;

__PACKAGE__->attr('tt');

Expand All @@ -31,14 +32,13 @@ sub _init {
# take and process options :-)

my %config = (
($app ? (INCLUDE_PATH => $app->home->rel_dir('templates')) : ()),
($app ? (INCLUDE_PATH => abs_path($app->home->rel_dir('templates'))) : ()),
COMPILE_EXT => '.ttc',
COMPILE_DIR => ($dir || File::Spec->tmpdir),
COMPILE_DIR => ($dir || abs_path(File::Spec->tmpdir)),
UNICODE => 1,
ENCODING => 'utf-8',
CACHE_SIZE => 128,
RELATIVE => 1,
ABSOLUTE => 1,
%{$args{template_options} || {}},
);

Expand All @@ -52,7 +52,6 @@ sub _init {
return $self;
}

use Data::Dumper;
sub _render {
my ($self, $renderer, $c, $output, $options) = @_;

Expand All @@ -64,10 +63,6 @@ sub _render {
$t = 'inline' if defined $inline;
return unless $t;

# Path
my $path = $renderer->template_path($options);
$path = b($inline)->md5_sum->to_string if defined $inline;
return unless $path;

my $helper = MojoX::Renderer::TT::Helper->new(ctx => $c);

Expand All @@ -77,14 +72,14 @@ sub _render {
my @params = ({%{$c->stash}, c => $c, h => $helper}, $output, {binmode => ':utf8'});
$self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c);

my $ok = $self->tt->process(defined $inline ? \$inline : $path, @params);
my $ok = $self->tt->process(defined $inline ? \$inline : $t, @params);

# Error
unless ($ok) {

my $e = Mojo::Exception->new(
$self->tt->error.'',
$self->tt->service->process(defined $inline ? \$inline : $path));
$self->tt->service->process(defined $inline ? \$inline : $t));
$$output = '';
$c->app->log->error(qq/Template error in "$t": $e/);
$c->render_exception($e);
Expand Down
4 changes: 3 additions & 1 deletion t/lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use strict;
use warnings;

BEGIN { $ENV{MOJO_MODE}='testing'; };

use utf8;

use Test::More tests => 31;
Expand Down Expand Up @@ -45,7 +47,7 @@ get '/inline' => sub { shift->render(inline => '[% 1 + 1 %]', handler => 'tt') }
my $t = Test::Mojo->new;

# Exception
$t->get_ok('/exception')->status_is(500)->content_like(qr/error/i);
$t->get_ok('/exception')->status_is(500)->content_like(qr/Exception/i);

# Normal rendering
$t->get_ok('/bar/hello')->content_is("hello");
Expand Down
8 changes: 8 additions & 0 deletions t/templates/exception.testing.html.ep
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
% my $s = $self->stash;
% my $e = $self->stash('exception');
% delete $s->{inner_template};
% delete $s->{exception};
% my $dump = dumper $s;
% $s->{exception} = $e;
An Exception has occured: <%= $e->message %>
at <%= $e->line->[0] %> in <%= $e->line->[1] %>

0 comments on commit 323d5a9

Please sign in to comment.