Fixed template path

This commit is contained in:
vti
2010-07-29 15:14:13 +03:00
parent 731fe52836
commit 9f26a20192
3 changed files with 33 additions and 19 deletions
+9 -7
View File
@@ -22,15 +22,17 @@ sub _init {
my $self = shift;
my %args = @_;
my $mojo = delete $args{mojo};
#$Template::Parser::DEBUG = 1;
my $dir = $mojo && $mojo->home->rel_dir('tmp/ctpl');
my $app = delete $args{mojo} || delete $args{app};
my $dir = $app && $app->home->rel_dir('tmp/ctpl');
# TODO
# take and process options :-)
my %config = (
($mojo ? (INCLUDE_PATH => $mojo->home->rel_dir('templates')) : ()),
($app ? (INCLUDE_PATH => $app->home->rel_dir('templates')) : ()),
COMPILE_EXT => '.ttc',
COMPILE_DIR => ($dir || File::Spec->tmpdir),
UNICODE => 1,
@@ -42,7 +44,7 @@ sub _init {
);
$config{LOAD_TEMPLATES} =
[MojoX::Renderer::TT::Provider->new(%config, renderer => $mojo->renderer)]
[MojoX::Renderer::TT::Provider->new(%config, renderer => $app->renderer)]
unless $config{LOAD_TEMPLATES};
$self->tt(Template->new(\%config))
@@ -133,8 +135,8 @@ sub new {
return $self;
}
sub renderer { @_ > 1 ? $_[0]->{renderer} = $_[1] : $_[0]->{renderer} }
sub ctx { @_ > 1 ? $_[0]->{ctx} = $_[1] : $_[0]->{ctx} }
sub renderer { @_ > 1 ? $_[0]->{renderer} = $_[1] : $_[0]->{renderer} }
sub ctx { @_ > 1 ? $_[0]->{ctx} = $_[1] : $_[0]->{ctx} }
sub _template_modified {1}
@@ -142,7 +144,7 @@ sub _template_content {
my $self = shift;
my ($path) = @_;
my $t = (File::Spec->splitpath($path))[2];
my ($t) = ($path =~ m{templates\/(.*)$});
if (-r $path) {
return $self->SUPER::_template_content(@_);
+1 -1
View File
@@ -12,7 +12,7 @@ sub register {
$args ||= {};
my $tt = MojoX::Renderer::TT->build(%$args, mojo => $app);
my $tt = MojoX::Renderer::TT->build(%$args, app => $app);
# Add "tt" handler
$app->renderer->add_handler(tt => $tt);
+23 -11
View File
@@ -16,7 +16,7 @@ app->log->level('fatal');
use_ok('MojoX::Renderer::TT');
plugin 'tt_renderer';
plugin 'tt_renderer' => {template_options => {PRE_CHOMP => 1, POST_CHOMP => 1, TRIM => 1}};
get '/exception' => 'error';
@@ -24,6 +24,8 @@ get '/with_include' => 'include';
get '/with_wrapper' => 'wrapper';
#get '/with_auto_wrapper' => sub { shift->render(auto_wrapper => layout => 'layout') };
get '/unicode' => 'unicode';
get '/helpers' => 'helpers';
@@ -40,25 +42,28 @@ my $t = Test::Mojo->new;
$t->get_ok('/exception')->status_is(500)->content_like(qr/error/i);
# Normal rendering
$t->get_ok('/foo/hello')->content_is("hello\n\n");
$t->get_ok('/foo/hello')->content_is("hello");
# With include
$t->get_ok('/with_include')->content_is("Hello\n\nInclude!\n\n");
$t->get_ok('/with_include')->content_is("HelloInclude!Hallo");
# With wrapper
$t->get_ok('/with_wrapper')->content_is("wrapped\n\n\n");
$t->get_ok('/with_wrapper')->content_is("wrapped");
# With auto wrapper
#$t->get_ok('/with_auto_wrapper')->content_is("wrapped");
# Unicode
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string . "\n\n");
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string);
# Helpers
$t->get_ok('/helpers')->content_is("/helpers\n\n");
$t->get_ok('/helpers')->content_is("/helpers");
# Unknown helper
$t->get_ok('/unknown_helper')->status_is(500)->content_like(qr//);
# On Disk
$t->get_ok('/on-disk')->content_is("4\n");
$t->get_ok('/on-disk')->content_is("4");
# Not found
$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);
@@ -74,17 +79,24 @@ __DATA__
@@ include.inc
Hello
@@ includes/include.inc
Hallo
@@ include.html.tt
[% INCLUDE 'include.inc' -%]
[%- INCLUDE 'include.inc' -%]
Include!
[% INCLUDE 'includes/include.inc' -%]
@@ layouts/layout.html.tt
w[%- content -%]d
@@ wrapper.html.tt
[%- WRAPPER 'layout.html.tt' -%]
[%- WRAPPER 'layouts/layout.html.tt' -%]
rappe
[%- END -%]
@@ layout.html.tt
w[% content %]d
@@ auto_wrapper.html.tt
rappe
@@ unicode.html.tt
привет