Added inline template support

This commit is contained in:
vti
2010-10-05 09:02:39 +02:00
parent be2d2ed0ec
commit 201774941a
4 changed files with 29 additions and 9 deletions

View File

@@ -24,15 +24,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
Mojo = 0.999930
Template = 2.18
[Prereqs / TestRequires ]

View File

@@ -5,8 +5,9 @@ use strict;
use base 'Mojo::Base';
use Template ();
use File::Spec ();
use Mojo::ByteStream 'b';
use Template ();
__PACKAGE__->attr('tt');
@@ -54,15 +55,25 @@ 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;
# 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 @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 : $path, @params);
# Error
unless ($ok) {
@@ -210,6 +221,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

@@ -5,7 +5,7 @@ use warnings;
use utf8;
use Test::More tests => 22;
use Test::More tests => 25;
use Mojolicious::Lite;
use Mojo::ByteStream 'b';
@@ -36,6 +36,8 @@ get '/on-disk' => 'foo';
get '/foo/:message' => 'index';
get '/inline' => sub { shift->render(inline => '[% 1 + 1 %]', handler => 'tt') };
my $t = Test::Mojo->new;
# Exception
@@ -68,6 +70,9 @@ $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