Compare commits

...

34 Commits

Author SHA1 Message Date
Ask Bjørn Hansen
9f20c780b8 Add Mojolicious-Plugin-TtRenderer-* to .gitignore 2011-08-01 09:35:17 -07:00
Ask Bjørn Hansen
c7c249b51f Update distribution name 2011-08-01 09:34:46 -07:00
Cosimo Streppone
dae8329981 [GH#12] Drop MojoX::* namespace
Renamed MojoX:: namespace to Mojolicious::Plugin::TtRenderer::Engine,
so there's ::Engine, ::Helper and ::Provider.

MojoX:: is deprecated and not to be used, according to @sri.
2011-08-01 11:56:25 +02:00
Ask Bjørn Hansen
5e650027c7 Mark 1.13 in Changes 2011-06-07 17:38:43 -07:00
Ask Bjørn Hansen
698fc48b58 Compatibility with Mojolicious 1.3+ 2011-06-07 17:38:10 -07:00
Ask Bjørn Hansen
49075ebe44 v1.12 2011-02-28 00:45:24 -08:00
Htbaa
0acdb2fcde Simplified regex and added comment why backslashes are being converted 2011-02-26 19:56:17 +01:00
Htbaa
dc52fec399 Inline wrappers and includes now work on Windows
closes gh-13
2011-02-26 12:46:02 +01:00
Ask Bjørn Hansen
a896a311bb Mark 1.11 in Changes 2011-02-06 16:00:58 -08:00
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
11 changed files with 182 additions and 55 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@ _build*
pm_to_blib*
*.tar.gz
.lwpcookies
Mojolicious-Plugin-TtRenderer-*
MojoX-Renderer-TT-*
cover_db
*~

38
Changes
View File

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

View File

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

View File

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

View File

@@ -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
View File

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

View File

@@ -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" );

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';
@@ -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
привет

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/);