Compare commits

...

25 Commits

Author SHA1 Message Date
Ask Bjørn Hansen
b6a0f10414 Update Changes 2011-02-06 15:59:56 -08:00
Ask Bjørn Hansen
68747b7f2f Merge remote branch 'marcusramberg/master' 2011-02-06 15:57:10 -08:00
Marcus Ramberg
323d5a9294 Change to use relative paths, add exception template for tests 2011-02-06 08:50:02 +01:00
Ask Bjørn Hansen
46715dc95e Mark 1.10 release in Changes 2011-02-05 12:25:48 -08:00
Ask Bjørn Hansen
392b97e81a Ignore .ttc files from tests 2011-02-05 12:22:44 -08:00
Ask Bjørn Hansen
a5e276f633 Update Changes 2011-02-05 12:20:42 -08:00
Ask Bjørn Hansen
fc453fe4fb Try fixing tests on Strawberry Perl (RT#65282)
(thanks Christiaan Kras)
2011-02-05 12:12:47 -08:00
Ask Bjørn Hansen
32c83d6109 Fix bugtracker URL in META.yml 2011-02-05 12:11:05 -08:00
Maxim Vuets
5e5c23d786 Make Mojolicious 'layout'/'extends' working 2011-02-05 10:02:09 -08:00
Ask Bjørn Hansen
fa30565b6b 1.0 released 2011-01-22 17:14:48 -08:00
Ask Bjørn Hansen
a0d8b522ff Update Changes 2011-01-22 17:13:27 -08:00
Ask Bjørn Hansen
388422491f Require Mojolicious 1.0+ 2011-01-22 10:47:00 -08:00
Ask Bjørn Hansen
1e7dc3044b Merge remote branch 'marcusramberg/master' 2011-01-22 10:33:09 -08:00
Marcus Ramberg
01b8f94a2b Fix inline rendering and test failures 2011-01-22 17:50:08 +01:00
Marcus Ramberg
26d555938f Fix unicode test 2011-01-22 14:42:42 +01:00
Marcus Ramberg
0d419c9ecd Support new mojolicious error template 2011-01-22 14:42:15 +01:00
Ask Bjørn Hansen
0708cfd051 Set repository and bug tracker URLs for the META.yml file 2011-01-17 10:19:11 -08:00
Ask Bjørn Hansen
3495571029 Update Changes 2010-12-27 14:53:35 -08:00
Árpád Szász
d51eea608b Fixed helper compatibility with Mojolicious 1.0 2010-12-28 00:28:50 +02:00
Marcus Ramberg
c3e8c9ff73 Tests for plugin 2010-10-10 06:02:54 +08:00
Maxim Vuets
0588884a9f Fix POD synopsis 2010-10-09 14:50:25 -07:00
spleenjack
084c79825f Replaced deprecated Mojo dependence with Mojolicious 2010-10-09 14:43:52 -07:00
Ask Bjørn Hansen
3bddf34485 'inline' argument is in stash, not in options for _render() 2010-10-05 00:38:44 -07:00
vti
201774941a Added inline template support 2010-10-05 09:02:39 +02:00
Ask Bjørn Hansen
be2d2ed0ec Add release date to Changes 2010-08-02 02:10:00 -07:00
9 changed files with 144 additions and 34 deletions

25
Changes
View File

@@ -1,6 +1,25 @@
Revision history for MojoX-Renderer-TT
{{$NEXT}}
- Change to use relative paths (Marcus Ramberg)
- Add exception template for tests
1.10 February 5, 2011
- Support Mojolicious 'layout'/'extends' (Maxim Vuets, Marcus
Ramberg)
- Fix Strawberry Perl tests (RT#65282, Christiaan Kras, Ask)
- Update bugtracker URL in META.yml
1.0 January 22, 2011
- 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)
- Added helpers (Viacheslav Tykhanovskyi)
- Documentation updates
@@ -22,15 +41,15 @@ Revision history for MojoX-Renderer-TT
- Updates to work with Mojo 0.9
- Add default INCLUDE_PATH
- Made default compile dir not Unix specific (Viacheslav
Tikhanovskii)
Tykhanovskyi)
0.10 November 12, 2008
- Update to work with named parameters in new version of Mojo
- Instead of "tx" pass the context in the "c" variable
- Rename "new" to "build"
- Add real tests (Viacheslav Tikhanovskii)
- Add real tests (Viacheslav Tykhanovskyi)
- Update to work with new error handling in Mojo
(Viacheslav Tikhanovskii)
(Viacheslav Tykhanovskyi)
0.02 November 10, 2008
- Add template_options parameter

View File

@@ -6,7 +6,7 @@ copyright_holder = Ask Bjørn Hansen
# copyright_year = 2009
[Prereqs]
Mojo = 0.999926
Mojolicious = 1.01
Template = 2.18
[Prereqs / TestRequires ]
@@ -21,8 +21,11 @@ bundle = @Basic
remove = Readme
remove = Manifest
[Repository]
[MetaResources]
bugtracker.web = https://github.com/abh/mojox-renderer-tt/issues
repository.web = http://github.com/abh/mojox-renderer-tt
repository.url = git://github.com/abh/mojox-renderer-tt.git
repository.type = git
[Homepage]

View File

@@ -5,8 +5,10 @@ use strict;
use base 'Mojo::Base';
use Template ();
use File::Spec ();
use Mojo::ByteStream 'b';
use Template ();
use Cwd qw/abs_path/;
__PACKAGE__->attr('tt');
@@ -30,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} || {}},
);
@@ -54,26 +55,31 @@ sub _init {
sub _render {
my ($self, $renderer, $c, $output, $options) = @_;
# Inline
my $inline = $options->{inline};
# Template
return unless my $t = $renderer->template_name($options);
return unless my $path = $renderer->template_path($options);
my $t = $renderer->template_name($options);
$t = 'inline' if defined $inline;
return unless $t;
my $helper = MojoX::Renderer::TT::Helper->new(ctx => $c);
# Purge previous result
$$output = '';
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($path, @params);
my $ok = $self->tt->process(defined $inline ? \$inline : $t, @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;
}
my $e = Mojo::Exception->new(
$self->tt->error.'',
$self->tt->service->process(defined $inline ? \$inline : $t));
$$output = '';
$c->app->log->error(qq/Template error in "$t": $e/);
$c->render_exception($e);
@@ -109,9 +115,9 @@ sub AUTOLOAD {
$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;
@@ -144,7 +150,7 @@ sub _template_content {
my $self = shift;
my ($path) = @_;
my ($t) = ($path =~ m{templates\/(.*)$});
my ($t) = ($path =~ m{templates[\/|\\](.*)$});
if (-r $path) {
return $self->SUPER::_template_content(@_);
@@ -179,7 +185,7 @@ Add the handler:
...
# Via mojolicious plugin
$self->plugin(tt_renderer => {FILTERS => [ ... ]});
$self->plugin(tt_renderer => {template_options => {FILTERS => [ ... ]}});
# Or manually
use MojoX::Renderer::TT;
@@ -210,6 +216,10 @@ When template file is not available rendering from C<__DATA__> is attempted.
@@ welcome.html.tt
Welcome, [% user.name %]!
Inline template is also supported:
$self->render(inline => '[% 1 + 1 %]', handler => 'tt');
=head1 HELPERS
Helpers are exported automatically under C<h> namespace.

View File

@@ -31,11 +31,11 @@ Mojolicious::Plugin::TtRenderer - Template Renderer Plugin
# Mojolicious
$self->plugin('tt_renderer');
$self->plugin(tt_renderer => {FILTERS => [ ... ]});
$self->plugin(tt_renderer => {template_options => {FILTERS => [ ... ]}});
# Mojolicious::Lite
plugin 'tt_renderer';
plugin tt_renderer => {FILTERS => [ ... ]};
plugin tt_renderer => {template_options => {FILTERS => [ ... ]}};
=head1 DESCRIPTION

2
t/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/tmp/
*.ttc

View File

@@ -3,9 +3,11 @@
use strict;
use warnings;
BEGIN { $ENV{MOJO_MODE}='testing'; };
use utf8;
use Test::More tests => 22;
use Test::More tests => 31;
use Mojolicious::Lite;
use Mojo::ByteStream 'b';
@@ -24,7 +26,11 @@ get '/with_include' => 'include';
get '/with_wrapper' => 'wrapper';
#get '/with_auto_wrapper' => sub { shift->render(auto_wrapper => layout => 'layout') };
get '/with_auto_wrapper' => 'auto_wrapper';
get '/inheritance_base' => 'inheritance_base';
get '/inheritance_derived' => 'inheritance_derived';
get '/unicode' => 'unicode';
@@ -34,15 +40,17 @@ get '/unknown_helper' => 'unknown_helper';
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;
# 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('/foo/hello')->content_is("hello");
$t->get_ok('/bar/hello')->content_is("hello");
# With include
$t->get_ok('/with_include')->content_is("HelloInclude!Hallo");
@@ -51,10 +59,14 @@ $t->get_ok('/with_include')->content_is("HelloInclude!Hallo");
$t->get_ok('/with_wrapper')->content_is("wrapped");
# With auto wrapper
#$t->get_ok('/with_auto_wrapper')->content_is("wrapped");
$t->get_ok('/with_auto_wrapper')->content_is("wrapped");
# Inheritance
$t->get_ok('/inheritance_base')->content_is("untouched");
$t->get_ok('/inheritance_derived')->content_is("edited");
# Unicode
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string);
$t->get_ok('/unicode')->content_is("привет");
# Helpers
$t->get_ok('/helpers')->content_is("/helpers");
@@ -68,9 +80,12 @@ $t->get_ok('/on-disk')->content_is("4");
# Not found
$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__
@@ index.html.tt
@@ bar.html.tt
[% message %]
@@ error.html.tt
@@ -95,9 +110,22 @@ w[%- content -%]d
rappe
[%- END -%]
@@ layouts/auto_layout.html.tt
w[%- h.content -%]d
@@ auto_wrapper.html.tt
[% CALL h.layout('auto_layout') %]
rappe
@@ inheritance_base.html.tt
[% verb = BLOCK %]untouch[% END %]
[% h.content('verb', verb) %]ed
@@ inheritance_derived.html.tt
[% CALL h.extends('inheritance_base') %]
[% verb = BLOCK %]edit[% END %]
[% h.content('verb', verb) %]
@@ unicode.html.tt
привет

View File

@@ -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] %>

View File

@@ -0,0 +1 @@
test123[% 456 %]

39
t/tt_plugin_lite_app.t Normal file
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/);