Made it more modern

This commit is contained in:
vti
2010-06-15 12:27:12 +02:00
parent a2ce996b97
commit 96d2be17b4
8 changed files with 223 additions and 72 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ WriteMakefile(
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'Mojo' => 0.991251,
'Mojo' => 0.999926,
'Template' => 2.18,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+85 -30
View File
@@ -2,13 +2,13 @@ package MojoX::Renderer::TT;
use warnings;
use strict;
use base 'Mojo::Base';
use Template ();
use Carp ();
use File::Spec ();
our $VERSION = '0.31';
our $VERSION = '0.32';
__PACKAGE__->attr('tt');
@@ -30,7 +30,7 @@ sub _init {
# take and process options :-)
my %config = (
( $mojo ? (INCLUDE_PATH => $mojo->home->rel_dir('templates') ) : () ),
($mojo ? (INCLUDE_PATH => $mojo->home->rel_dir('templates')) : ()),
COMPILE_EXT => '.ttc',
COMPILE_DIR => ($dir || File::Spec->tmpdir),
UNICODE => 1,
@@ -41,6 +41,9 @@ sub _init {
%{$args{template_options} || {}},
);
$config{LOAD_TEMPLATES} = [Mojo::Template::Provider->new(%config, renderer => $mojo->renderer)]
unless $config{LOAD_TEMPLATES};
$self->tt(Template->new(\%config))
or Carp::croak "Could not initialize Template object: $Template::ERROR";
@@ -50,29 +53,82 @@ sub _init {
sub _render {
my ($self, $renderer, $c, $output, $options) = @_;
my $template_path;
unless($template_path = $c->stash->{'template_path'}) {
$template_path = $renderer->template_path($options);
}
# Template
return unless my $t = $renderer->template_name($options);
return unless my $path = $renderer->template_path($options);
unless (
$self->tt->process(
$template_path, {%{$c->stash}, c => $c},
$output, {binmode => ":utf8"}
)
)
{
Carp::carp $self->tt->error . "\n";
my @params = ({%{$c->stash}, c => $c}, $output, {binmode => ':utf8'});
$self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c);
my $ok = $self->tt->process($path, @params);
# Error
unless ($ok) {
my $e = $self->tt->error;
if ($e =~ m/not found/) {
$c->app->log->error(qq/Template "$t" missing or not readable./);
$c->render_not_found;
return;
}
$$output = '';
$c->app->log->error(qq/Template error in "$t": $e/);
$c->render_exception($e);
$self->tt->error('');
return 0;
}
else {
return 1;
}
return 1;
}
1; # End of MojoX::Renderer::TT
package Mojo::Template::Provider;
use strict;
use warnings;
use base 'Template::Provider';
sub new {
my $class = shift;
my %params = @_;
my $renderer = delete $params{renderer};
my $self = $class->SUPER::new(%params);
$self->renderer($renderer);
return $self;
}
sub renderer { @_ > 1 ? $_[0]->{renderer} = $_[1] : $_[0]->{renderer} }
sub ctx { @_ > 1 ? $_[0]->{ctx} = $_[1] : $_[0]->{ctx} }
sub _template_modified {1}
sub _template_content {
my $self = shift;
my ($path) = @_;
my $t = (File::Spec->splitpath($path))[2];
if (-r $path) {
return $self->SUPER::_template_content(@_);
}
# Try DATA section
elsif (my $d = $self->renderer->get_inline_template($self->ctx, $t)) {
return wantarray ? ($d, '', time) : $d;
}
my $data = '';
my $error = "$path: not found";
my $mod_date = time;
return wantarray ? ($data, $error, $mod_date) : $data;
}
1;
__END__
=encoding utf-8
@@ -102,6 +158,9 @@ Add the handler:
$self->renderer->add_handler( tt => $tt );
}
# Or using a plugin
$self->plugin(tt_renderer => {FILTERS => [ ... ]});
And then in the handler call render which will call the
MojoX::Renderer::TT renderer.
@@ -127,11 +186,10 @@ object. When used the INCLUDE_PATH will be set to
=item template_options
A hash reference of options that are passed to Template->new().
A hash reference of options that are passed to Template->new().
=back
=head1 AUTHOR
Ask Bjørn Hansen, C<< <ask at develooper.com> >>
@@ -145,12 +203,11 @@ Ask Bjørn Hansen, C<< <ask at develooper.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-mojox-renderer-tt at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Renderer-TT>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
Please report any bugs or feature requests to C<bug-mojox-renderer-tt at rt.cpan.org>,
or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Renderer-TT>. I will be
notified, and then you'll automatically be notified of progress on your bug as I
make changes.
=head1 SUPPORT
@@ -183,16 +240,14 @@ L<http://search.cpan.org/dist/MojoX-Renderer-TT/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2008-2009 Ask Bjørn Hansen, all rights reserved.
Copyright 2008-2010 Ask Bjørn Hansen, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
+57
View File
@@ -0,0 +1,57 @@
package Mojolicious::Plugin::TtRenderer;
use strict;
use warnings;
use base 'Mojolicious::Plugin';
use MojoX::Renderer::TT;
sub register {
my ($self, $app, $args) = @_;
$args ||= {};
my $tt = MojoX::Renderer::TT->build(%$args, mojo => $app);
# Add "tt" handler
$app->renderer->add_handler(tt => $tt);
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::TtRenderer - Template Renderer Plugin
=head1 SYNOPSIS
# Mojolicious
$self->plugin('tt_renderer');
$self->plugin(tt_renderer => {FILTERS => [ ... ]});
# Mojolicious::Lite
plugin 'tt_renderer';
plugin tt_renderer => {FILTERS => [ ... ]};
=head1 DESCRIPTION
L<Mojolicous::Plugin::TtRenderer> is a simple loader for L<MojoX::Renderer::TT>.
=head1 METHODS
L<Mojolicious::Plugin::TtRenderer> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
$plugin->register;
Register renderer in L<Mojolicious> application.
=head1 SEE ALSO
L<MojoX::Renderer::TT>, L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Test::More tests => 17;
use Mojolicious::Lite;
use Mojo::ByteStream 'b';
use Test::Mojo;
# Silence
app->log->level('fatal');
use_ok('MojoX::Renderer::TT');
plugin 'tt_renderer';
get '/exception' => 'error';
get '/with_include' => 'include';
get '/with_wrapper' => 'wrapper';
get '/unicode' => 'unicode';
get '/on-disk' => 'foo';
get '/foo/:message' => 'index';
my $t = Test::Mojo->new;
# Exception
$t->get_ok('/exception')->status_is(500)->content_like(qr/error/i);
# Normal rendering
$t->get_ok('/foo/hello')->content_is("hello\n\n");
# With include
$t->get_ok('/with_include')->content_is("Hello\n\nInclude!\n\n");
# With wrapper
$t->get_ok('/with_wrapper')->content_is("wrapped\n\n\n");
# Unicode
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string . "\n");
# On Disk
$t->get_ok('/on-disk')->content_is("4\n");
# Not found
$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);
__DATA__
@@ index.html.tt
[% message %]
@@ error.html.tt
[% 1 + % %]
@@ include.inc
Hello
@@ include.html.tt
[% INCLUDE 'include.inc' -%]
Include!
@@ wrapper.html.tt
[%- WRAPPER 'layout.html.tt' -%]
rappe
[%- END -%]
@@ layout.html.tt
w[% content %]d
@@ unicode.html.tt
привет
-39
View File
@@ -1,39 +0,0 @@
#!perl
use strict;
use warnings;
use Test::More tests => 6;
use Mojolicious;
use Mojolicious::Controller;
use MojoX::Renderer;
use_ok('MojoX::Renderer::TT');
my $c = Mojolicious::Controller->new(app => Mojolicious->new);
$c->app->log->path(undef);
$c->app->log->level('fatal');
my $mt = MojoX::Renderer::TT->build;
my $output;
my $rv;
$c->stash->{template_path} = 't/render/template.tt2';
$rv = $mt->(undef, $c, \$output);
is($rv, 1);
is($output, "4\n");
$c->stash->{template_path} = 't/render/error.tt2';
$rv = $mt->(undef, $c, \$output);
is($rv, 0);
ok($output);
delete $c->stash->{template_path};
$c->app->renderer->root('./')->add_handler(tt2 => $mt);
is($c->render('t/render/template','partial' => 1,handler => 'tt2'),"4\n");
-1
View File
@@ -1 +0,0 @@
[% = 2 %]
-1
View File
@@ -1 +0,0 @@
[% 2 + 2 %]