Compare commits
34 Commits
release/0.
...
release/1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f20c780b8 | ||
|
|
c7c249b51f | ||
|
|
dae8329981 | ||
|
|
5e650027c7 | ||
|
|
698fc48b58 | ||
|
|
49075ebe44 | ||
|
|
0acdb2fcde | ||
|
|
dc52fec399 | ||
|
|
a896a311bb | ||
|
|
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 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,6 +6,7 @@ _build*
|
||||
pm_to_blib*
|
||||
*.tar.gz
|
||||
.lwpcookies
|
||||
Mojolicious-Plugin-TtRenderer-*
|
||||
MojoX-Renderer-TT-*
|
||||
cover_db
|
||||
*~
|
||||
|
||||
38
Changes
38
Changes
@@ -1,6 +1,38 @@
|
||||
Revision history for MojoX-Renderer-TT
|
||||
|
||||
{{$NEXT}}
|
||||
|
||||
1.14 August 1, 2011
|
||||
- Drop use of MojoX namespace for Mojolicious::Plugin::TtRenderer::
|
||||
(GH#12, Cosimo Streppone)
|
||||
|
||||
1.13 June 7, 2011
|
||||
- Compatibility with Mojolicious 1.3+
|
||||
|
||||
1.12 February 28, 2011
|
||||
- Inline wrappers and includes now work on Windows
|
||||
(GH#13, Christiaan Kras)
|
||||
|
||||
1.11 February 6, 2011
|
||||
- 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 +54,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
|
||||
|
||||
11
dist.ini
11
dist.ini
@@ -1,4 +1,4 @@
|
||||
name = MojoX-Renderer-TT
|
||||
name = Mojolicious-Plugin-TtRenderer
|
||||
author = Ask Bjørn Hansen <ask@develooper.com>
|
||||
license = Perl_5
|
||||
|
||||
@@ -6,7 +6,7 @@ copyright_holder = Ask Bjørn Hansen
|
||||
# copyright_year = 2009
|
||||
|
||||
[Prereqs]
|
||||
Mojo = 0.999926
|
||||
Mojolicious = 1.3
|
||||
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]
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@ our $VERSION = '0.01';
|
||||
|
||||
use base 'Mojolicious::Plugin';
|
||||
|
||||
use MojoX::Renderer::TT;
|
||||
use Mojolicious::Plugin::TtRenderer::Engine;
|
||||
|
||||
sub register {
|
||||
my ($self, $app, $args) = @_;
|
||||
|
||||
$args ||= {};
|
||||
|
||||
my $tt = MojoX::Renderer::TT->build(%$args, app => $app);
|
||||
my $tt = Mojolicious::Plugin::TtRenderer::Engine->build(%$args, app => $app);
|
||||
|
||||
# Add "tt" handler
|
||||
$app->renderer->add_handler(tt => $tt);
|
||||
@@ -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
|
||||
|
||||
@@ -54,6 +54,6 @@ Register renderer in L<Mojolicious> application.
|
||||
|
||||
=head1 SEE ALSO
|
||||
|
||||
L<MojoX::Renderer::TT>, L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
|
||||
L<Mojolicious::Plugin::TtRenderer::Engine>, L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
|
||||
|
||||
=cut
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package MojoX::Renderer::TT;
|
||||
package Mojolicious::Plugin::TtRenderer::Engine;
|
||||
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
use base 'Mojo::Base';
|
||||
|
||||
use Template ();
|
||||
use Carp ();
|
||||
use File::Spec ();
|
||||
use Mojo::ByteStream 'b';
|
||||
use Template ();
|
||||
use Cwd qw/abs_path/;
|
||||
|
||||
__PACKAGE__->attr('tt');
|
||||
|
||||
@@ -30,19 +33,18 @@ 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} || {}},
|
||||
);
|
||||
|
||||
$config{LOAD_TEMPLATES} =
|
||||
[MojoX::Renderer::TT::Provider->new(%config, renderer => $app->renderer)]
|
||||
[Mojolicious::Plugin::TtRenderer::Provider->new(%config, renderer => $app->renderer)]
|
||||
unless $config{LOAD_TEMPLATES};
|
||||
|
||||
$self->tt(Template->new(\%config))
|
||||
@@ -54,26 +56,31 @@ sub _init {
|
||||
sub _render {
|
||||
my ($self, $renderer, $c, $output, $options) = @_;
|
||||
|
||||
# Template
|
||||
return unless my $t = $renderer->template_name($options);
|
||||
return unless my $path = $renderer->template_path($options);
|
||||
# Inline
|
||||
my $inline = $options->{inline};
|
||||
|
||||
my $helper = MojoX::Renderer::TT::Helper->new(ctx => $c);
|
||||
# Template
|
||||
my $t = $renderer->template_name($options);
|
||||
$t = 'inline' if defined $inline;
|
||||
return unless $t;
|
||||
|
||||
|
||||
my $helper = Mojolicious::Plugin::TtRenderer::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);
|
||||
@@ -84,10 +91,10 @@ sub _render {
|
||||
return 1;
|
||||
}
|
||||
|
||||
1; # End of MojoX::Renderer::TT
|
||||
1; # End of Mojolicious::Plugin::TtRenderer::Engine
|
||||
|
||||
package
|
||||
MojoX::Renderer::TT::Helper;
|
||||
Mojolicious::Plugin::TtRenderer::Helper;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
@@ -109,15 +116,15 @@ 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;
|
||||
|
||||
package
|
||||
MojoX::Renderer::TT::Provider;
|
||||
Mojolicious::Plugin::TtRenderer::Provider;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
@@ -144,6 +151,8 @@ sub _template_content {
|
||||
my $self = shift;
|
||||
my ($path) = @_;
|
||||
|
||||
# Convert backslashes to forward slashes to make inline templates work on Windows
|
||||
$path =~ s/\\/\//g;
|
||||
my ($t) = ($path =~ m{templates\/(.*)$});
|
||||
|
||||
if (-r $path) {
|
||||
@@ -151,7 +160,7 @@ sub _template_content {
|
||||
}
|
||||
|
||||
# Try DATA section
|
||||
elsif (my $d = $self->renderer->get_inline_template($self->ctx, $t)) {
|
||||
elsif (my $d = $self->renderer->get_data_template($self->ctx, $t)) {
|
||||
return wantarray ? ($d, '', time) : $d;
|
||||
}
|
||||
|
||||
@@ -169,7 +178,7 @@ __END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
MojoX::Renderer::TT - Template Toolkit renderer for Mojo
|
||||
Mojolicious::Plugin::TtRenderer::Engine - Template Toolkit renderer for Mojo
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
@@ -179,12 +188,12 @@ Add the handler:
|
||||
...
|
||||
|
||||
# Via mojolicious plugin
|
||||
$self->plugin(tt_renderer => {FILTERS => [ ... ]});
|
||||
$self->plugin(tt_renderer => {template_options => {FILTERS => [ ... ]}});
|
||||
|
||||
# Or manually
|
||||
use MojoX::Renderer::TT;
|
||||
use Mojolicious::Plugin::TtRenderer::Engine;
|
||||
|
||||
my $tt = MojoX::Renderer::TT->build(
|
||||
my $tt = Mojolicious::Plugin::TtRenderer::Engine->build(
|
||||
mojo => $self,
|
||||
template_options => {
|
||||
PROCESS => 'tpl/wrapper',
|
||||
@@ -210,6 +219,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.
|
||||
@@ -228,7 +241,7 @@ Supported parameters are
|
||||
|
||||
=item mojo
|
||||
C<build> currently uses a C<mojo> parameter pointing to the base class (Mojo).
|
||||
object. When used the INCLUDE_PATH will be set to
|
||||
object. When used the INCLUDE_PATH will be set to
|
||||
|
||||
=item template_options
|
||||
|
||||
@@ -259,7 +272,7 @@ make changes.
|
||||
|
||||
You can find documentation for this module with the perldoc command.
|
||||
|
||||
perldoc MojoX::Renderer::TT
|
||||
perldoc Mojolicious::Plugin::TtRenderer::Engine
|
||||
|
||||
You can also look for information at:
|
||||
|
||||
2
t/.gitignore
vendored
Normal file
2
t/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/tmp/
|
||||
*.ttc
|
||||
@@ -3,7 +3,7 @@
|
||||
use Test::More tests => 1;
|
||||
|
||||
BEGIN {
|
||||
use_ok( 'MojoX::Renderer::TT' );
|
||||
use_ok( 'Mojolicious::Plugin::TtRenderer::Engine' );
|
||||
}
|
||||
|
||||
diag( "Testing MojoX::Renderer::TT $MojoX::Renderer::TT::VERSION, Perl $], $^X" );
|
||||
diag( "Testing Mojolicious::Plugin::TtRenderer::Engine $Mojolicious::Plugin::TtRenderer::Engine::VERSION, Perl $], $^X" );
|
||||
|
||||
50
t/lite_app.t
50
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';
|
||||
@@ -14,7 +16,7 @@ use Test::Mojo;
|
||||
# Silence
|
||||
app->log->level('fatal');
|
||||
|
||||
use_ok('MojoX::Renderer::TT');
|
||||
use_ok('Mojolicious::Plugin::TtRenderer::Engine');
|
||||
|
||||
plugin 'tt_renderer' => {template_options => {PRE_CHOMP => 1, POST_CHOMP => 1, TRIM => 1}};
|
||||
|
||||
@@ -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
|
||||
@@ -79,13 +94,13 @@ __DATA__
|
||||
@@ include.inc
|
||||
Hello
|
||||
|
||||
@@ includes/include.inc
|
||||
@@ includes/sub/include.inc
|
||||
Hallo
|
||||
|
||||
@@ include.html.tt
|
||||
[%- INCLUDE 'include.inc' -%]
|
||||
Include!
|
||||
[% INCLUDE 'includes/include.inc' -%]
|
||||
[% INCLUDE 'includes/sub/include.inc' -%]
|
||||
|
||||
@@ layouts/layout.html.tt
|
||||
w[%- content -%]d
|
||||
@@ -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