From 96d2be17b4d840991d2b15b0af7b1e6ee31633be Mon Sep 17 00:00:00 2001 From: vti Date: Tue, 15 Jun 2010 12:27:12 +0200 Subject: [PATCH] Made it more modern --- Makefile.PL | 2 +- lib/MojoX/Renderer/TT.pm | 115 +++++++++++++----- lib/Mojolicious/Plugin/TtRenderer.pm | 57 +++++++++ t/lite_app.t | 80 ++++++++++++ t/render.t | 39 ------ t/render/template-error.tt2 | 1 - t/render/template.tt2 | 1 - .../foo.html.tt} | 0 8 files changed, 223 insertions(+), 72 deletions(-) create mode 100644 lib/Mojolicious/Plugin/TtRenderer.pm create mode 100644 t/lite_app.t delete mode 100644 t/render.t delete mode 100644 t/render/template-error.tt2 delete mode 100644 t/render/template.tt2 rename t/{render/template.html.tt2 => templates/foo.html.tt} (100%) diff --git a/Makefile.PL b/Makefile.PL index 9273651..c47379b 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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', }, diff --git a/lib/MojoX/Renderer/TT.pm b/lib/MojoX/Renderer/TT.pm index 98ac1a8..7878681 100644 --- a/lib/MojoX/Renderer/TT.pm +++ b/lib/MojoX/Renderer/TT.pm @@ -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<< >> @@ -145,12 +203,11 @@ Ask Bjørn Hansen, C<< >> =head1 BUGS -Please report any bugs or feature requests to C, or through -the web interface at L. 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, +or through the web interface at +L. 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 =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 diff --git a/lib/Mojolicious/Plugin/TtRenderer.pm b/lib/Mojolicious/Plugin/TtRenderer.pm new file mode 100644 index 0000000..9404fd3 --- /dev/null +++ b/lib/Mojolicious/Plugin/TtRenderer.pm @@ -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 is a simple loader for L. + +=head1 METHODS + +L inherits all methods from +L and implements the following new ones. + +=head2 C + + $plugin->register; + +Register renderer in L application. + +=head1 SEE ALSO + +L, L, L, L. + +=cut diff --git a/t/lite_app.t b/t/lite_app.t new file mode 100644 index 0000000..43ea23b --- /dev/null +++ b/t/lite_app.t @@ -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 +привет diff --git a/t/render.t b/t/render.t deleted file mode 100644 index 85dd8ec..0000000 --- a/t/render.t +++ /dev/null @@ -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"); - - diff --git a/t/render/template-error.tt2 b/t/render/template-error.tt2 deleted file mode 100644 index 6047416..0000000 --- a/t/render/template-error.tt2 +++ /dev/null @@ -1 +0,0 @@ -[% = 2 %] diff --git a/t/render/template.tt2 b/t/render/template.tt2 deleted file mode 100644 index 4c06c07..0000000 --- a/t/render/template.tt2 +++ /dev/null @@ -1 +0,0 @@ -[% 2 + 2 %] diff --git a/t/render/template.html.tt2 b/t/templates/foo.html.tt similarity index 100% rename from t/render/template.html.tt2 rename to t/templates/foo.html.tt