Compare commits
25 Commits
release/0.
...
release/1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6a0f10414 | ||
|
|
68747b7f2f | ||
|
|
323d5a9294 | ||
|
|
46715dc95e | ||
|
|
392b97e81a | ||
|
|
a5e276f633 | ||
|
|
fc453fe4fb | ||
|
|
32c83d6109 | ||
|
|
5e5c23d786 | ||
|
|
fa30565b6b | ||
|
|
a0d8b522ff | ||
|
|
388422491f | ||
|
|
1e7dc3044b | ||
|
|
01b8f94a2b | ||
|
|
26d555938f | ||
|
|
0d419c9ecd | ||
|
|
0708cfd051 | ||
|
|
3495571029 | ||
|
|
d51eea608b | ||
|
|
c3e8c9ff73 | ||
|
|
0588884a9f | ||
|
|
084c79825f | ||
|
|
3bddf34485 | ||
|
|
201774941a | ||
|
|
be2d2ed0ec |
25
Changes
25
Changes
@@ -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
|
||||
|
||||
9
dist.ini
9
dist.ini
@@ -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]
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
2
t/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/tmp/
|
||||
*.ttc
|
||||
44
t/lite_app.t
44
t/lite_app.t
@@ -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
|
||||
привет
|
||||
|
||||
|
||||
8
t/templates/exception.testing.html.ep
Normal file
8
t/templates/exception.testing.html.ep
Normal 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] %>
|
||||
1
t/templates/index.html.tt
Normal file
1
t/templates/index.html.tt
Normal file
@@ -0,0 +1 @@
|
||||
test123[% 456 %]
|
||||
39
t/tt_plugin_lite_app.t
Normal file
39
t/tt_plugin_lite_app.t
Normal 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/);
|
||||
Reference in New Issue
Block a user