Compare commits

...
7 changed files with 95 additions and 25 deletions
+12 -3
View File
@@ -1,6 +1,15 @@
Revision history for MojoX-Renderer-TT Revision history for MojoX-Renderer-TT
{{$NEXT}} {{$NEXT}}
- Require (and be compatible with) Mojolicious 1.0+
- Added inline template support (Viacheslav Tykhanovskyi & Ask)
- Fix inline rendering and test failures (Marcus Ramberg)
- Fixed helper compatibility with Mojolicious 1.0 (Árpád Szász)
- Support new mojolicious error template (Marcus)
- Fix unicode test (Marcus & Spleenjack)
- Replaced deprecated Mojo dependence with Mojolicious (spleenjack)
0.40 August 2, 2010
- Updated for newer Mojo (Viacheslav Tykhanovskyi) - Updated for newer Mojo (Viacheslav Tykhanovskyi)
- Added helpers (Viacheslav Tykhanovskyi) - Added helpers (Viacheslav Tykhanovskyi)
- Documentation updates - Documentation updates
@@ -22,15 +31,15 @@ Revision history for MojoX-Renderer-TT
- Updates to work with Mojo 0.9 - Updates to work with Mojo 0.9
- Add default INCLUDE_PATH - Add default INCLUDE_PATH
- Made default compile dir not Unix specific (Viacheslav - Made default compile dir not Unix specific (Viacheslav
Tikhanovskii) Tykhanovskyi)
0.10 November 12, 2008 0.10 November 12, 2008
- Update to work with named parameters in new version of Mojo - Update to work with named parameters in new version of Mojo
- Instead of "tx" pass the context in the "c" variable - Instead of "tx" pass the context in the "c" variable
- Rename "new" to "build" - Rename "new" to "build"
- Add real tests (Viacheslav Tikhanovskii) - Add real tests (Viacheslav Tykhanovskyi)
- Update to work with new error handling in Mojo - Update to work with new error handling in Mojo
(Viacheslav Tikhanovskii) (Viacheslav Tykhanovskyi)
0.02 November 10, 2008 0.02 November 10, 2008
- Add template_options parameter - Add template_options parameter
+5 -1
View File
@@ -6,7 +6,7 @@ copyright_holder = Ask Bjørn Hansen
# copyright_year = 2009 # copyright_year = 2009
[Prereqs] [Prereqs]
Mojo = 0.999926 Mojolicious = 1.01
Template = 2.18 Template = 2.18
[Prereqs / TestRequires ] [Prereqs / TestRequires ]
@@ -22,7 +22,11 @@ remove = Readme
remove = Manifest remove = Manifest
[Repository] [Repository]
repository = git://github.com/abh/mojox-renderer-tt.git
web = https://github.com/abh/mojox-renderer-tt
[Bugtracker]
web = https://github.com/abh/mojox-renderer-tt/issues
[Homepage] [Homepage]
+26 -14
View File
@@ -5,8 +5,9 @@ use strict;
use base 'Mojo::Base'; use base 'Mojo::Base';
use Template ();
use File::Spec (); use File::Spec ();
use Mojo::ByteStream 'b';
use Template ();
__PACKAGE__->attr('tt'); __PACKAGE__->attr('tt');
@@ -51,29 +52,36 @@ sub _init {
return $self; return $self;
} }
use Data::Dumper;
sub _render { sub _render {
my ($self, $renderer, $c, $output, $options) = @_; my ($self, $renderer, $c, $output, $options) = @_;
# Inline
my $inline = $options->{inline};
# Template # Template
return unless my $t = $renderer->template_name($options); my $t = $renderer->template_name($options);
return unless my $path = $renderer->template_path($options); $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); my $helper = MojoX::Renderer::TT::Helper->new(ctx => $c);
my @params = ({%{$c->stash}, c => $c, h => $helper}, $output, {binmode => ':utf8'}); my @params = ({%{$c->stash}, c => $c, h => $helper}, $output, {binmode => ':utf8'});
$self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c); $self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c);
my $ok = $self->tt->process($path, @params);
my $ok = $self->tt->process(defined $inline ? \$inline : $path, @params);
# Error # Error
unless ($ok) { 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;
}
my $e = Mojo::Exception->new(
$self->tt->error.'',
$self->tt->service->process(defined $inline ? \$inline : $path));
$$output = ''; $$output = '';
$c->app->log->error(qq/Template error in "$t": $e/); $c->app->log->error(qq/Template error in "$t": $e/);
$c->render_exception($e); $c->render_exception($e);
@@ -109,9 +117,9 @@ sub AUTOLOAD {
$method = (split '::' => $method)[-1]; $method = (split '::' => $method)[-1];
die qq/Unknown helper: $method/ unless $self->ctx->app->renderer->helper->{$method}; die qq/Unknown helper: $method/ unless $self->ctx->app->renderer->helpers->{$method};
return $self->ctx->helper($method => @_); return $self->ctx->$method(@_);
} }
1; 1;
@@ -179,7 +187,7 @@ Add the handler:
... ...
# Via mojolicious plugin # Via mojolicious plugin
$self->plugin(tt_renderer => {FILTERS => [ ... ]}); $self->plugin(tt_renderer => {template_options => {FILTERS => [ ... ]}});
# Or manually # Or manually
use MojoX::Renderer::TT; use MojoX::Renderer::TT;
@@ -210,6 +218,10 @@ When template file is not available rendering from C<__DATA__> is attempted.
@@ welcome.html.tt @@ welcome.html.tt
Welcome, [% user.name %]! Welcome, [% user.name %]!
Inline template is also supported:
$self->render(inline => '[% 1 + 1 %]', handler => 'tt');
=head1 HELPERS =head1 HELPERS
Helpers are exported automatically under C<h> namespace. Helpers are exported automatically under C<h> namespace.
+2 -2
View File
@@ -31,11 +31,11 @@ Mojolicious::Plugin::TtRenderer - Template Renderer Plugin
# Mojolicious # Mojolicious
$self->plugin('tt_renderer'); $self->plugin('tt_renderer');
$self->plugin(tt_renderer => {FILTERS => [ ... ]}); $self->plugin(tt_renderer => {template_options => {FILTERS => [ ... ]}});
# Mojolicious::Lite # Mojolicious::Lite
plugin 'tt_renderer'; plugin 'tt_renderer';
plugin tt_renderer => {FILTERS => [ ... ]}; plugin tt_renderer => {template_options => {FILTERS => [ ... ]}};
=head1 DESCRIPTION =head1 DESCRIPTION
+10 -5
View File
@@ -5,7 +5,7 @@ use warnings;
use utf8; use utf8;
use Test::More tests => 22; use Test::More tests => 25;
use Mojolicious::Lite; use Mojolicious::Lite;
use Mojo::ByteStream 'b'; use Mojo::ByteStream 'b';
@@ -34,7 +34,9 @@ get '/unknown_helper' => 'unknown_helper';
get '/on-disk' => 'foo'; get '/on-disk' => 'foo';
get '/foo/:message' => 'index'; get '/bar/:message' => 'bar';
get '/inline' => sub { shift->render(inline => '[% 1 + 1 %]', handler => 'tt') };
my $t = Test::Mojo->new; my $t = Test::Mojo->new;
@@ -42,7 +44,7 @@ my $t = Test::Mojo->new;
$t->get_ok('/exception')->status_is(500)->content_like(qr/error/i); $t->get_ok('/exception')->status_is(500)->content_like(qr/error/i);
# Normal rendering # Normal rendering
$t->get_ok('/foo/hello')->content_is("hello"); $t->get_ok('/bar/hello')->content_is("hello");
# With include # With include
$t->get_ok('/with_include')->content_is("HelloInclude!Hallo"); $t->get_ok('/with_include')->content_is("HelloInclude!Hallo");
@@ -54,7 +56,7 @@ $t->get_ok('/with_wrapper')->content_is("wrapped");
#$t->get_ok('/with_auto_wrapper')->content_is("wrapped"); #$t->get_ok('/with_auto_wrapper')->content_is("wrapped");
# Unicode # Unicode
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string); $t->get_ok('/unicode')->content_is("привет");
# Helpers # Helpers
$t->get_ok('/helpers')->content_is("/helpers"); $t->get_ok('/helpers')->content_is("/helpers");
@@ -68,9 +70,12 @@ $t->get_ok('/on-disk')->content_is("4");
# Not found # Not found
$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i); $t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);
# Inline
$t->get_ok('/inline')->status_is(200)->content_is('2');
__DATA__ __DATA__
@@ index.html.tt @@ bar.html.tt
[% message %] [% message %]
@@ error.html.tt @@ error.html.tt
+1
View File
@@ -0,0 +1 @@
test123[% 456 %]
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env perl
# Copyright (C) 2008-2010, Sebastian Riedel.
use strict;
use warnings;
use File::Temp;
use Mojo::IOLoop;
use Test::More;
# Use a clean temporary directory
BEGIN { $ENV{MOJO_TMPDIR} ||= File::Temp::tempdir }
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 3;
# Leela: OK, this has gotta stop. I'm going to remind Fry of his humanity the way only a woman can.
# Farnsworth: You're going to do his laundry?
use Mojolicious::Lite;
use Test::Mojo;
# POD renderer plugin
plugin 'tt_renderer';
# Silence
app->log->level('error');
# GET /
get '/' => 'index';
my $t = Test::Mojo->new;
# Simple TT template
$t->get_ok('/')->status_is(200)
->content_like(qr/test123456/);