Added helpers
This commit is contained in:
@@ -58,7 +58,9 @@ sub _render {
|
||||
return unless my $t = $renderer->template_name($options);
|
||||
return unless my $path = $renderer->template_path($options);
|
||||
|
||||
my @params = ({%{$c->stash}, c => $c}, $output, {binmode => ':utf8'});
|
||||
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);
|
||||
|
||||
@@ -84,6 +86,33 @@ sub _render {
|
||||
|
||||
1; # End of MojoX::Renderer::TT
|
||||
|
||||
package MojoX::Renderer::TT::Helper;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use base 'Mojo::Base';
|
||||
|
||||
our $AUTOLOAD;
|
||||
|
||||
__PACKAGE__->attr('ctx');
|
||||
|
||||
sub AUTOLOAD {
|
||||
my $self = shift;
|
||||
|
||||
my $method = $AUTOLOAD;
|
||||
|
||||
return if $method =~ /^[A-Z]+?$/;
|
||||
return if $method =~ /^_/;
|
||||
return if $method =~ /(?:\:*?)DESTROY$/;
|
||||
|
||||
$method = (split '::' => $method)[-1];
|
||||
|
||||
return $self->ctx->helper($method => @_);
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
package MojoX::Renderer::TT::Provider;
|
||||
|
||||
use strict;
|
||||
|
||||
+10
-2
@@ -5,7 +5,7 @@ use warnings;
|
||||
|
||||
use utf8;
|
||||
|
||||
use Test::More tests => 17;
|
||||
use Test::More tests => 19;
|
||||
|
||||
use Mojolicious::Lite;
|
||||
use Mojo::ByteStream 'b';
|
||||
@@ -26,6 +26,8 @@ get '/with_wrapper' => 'wrapper';
|
||||
|
||||
get '/unicode' => 'unicode';
|
||||
|
||||
get '/helpers' => 'helpers';
|
||||
|
||||
get '/on-disk' => 'foo';
|
||||
|
||||
get '/foo/:message' => 'index';
|
||||
@@ -45,7 +47,10 @@ $t->get_ok('/with_include')->content_is("Hello\n\nInclude!\n\n");
|
||||
$t->get_ok('/with_wrapper')->content_is("wrapped\n\n\n");
|
||||
|
||||
# Unicode
|
||||
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string . "\n");
|
||||
$t->get_ok('/unicode')->content_is(b("привет")->encode('UTF-8')->to_string . "\n\n");
|
||||
|
||||
# Helpers
|
||||
$t->get_ok('/helpers')->content_is("/helpers\n");
|
||||
|
||||
# On Disk
|
||||
$t->get_ok('/on-disk')->content_is("4\n");
|
||||
@@ -78,3 +83,6 @@ w[% content %]d
|
||||
|
||||
@@ unicode.html.tt
|
||||
привет
|
||||
|
||||
@@ helpers.html.tt
|
||||
[% h.url_for('helpers') %]
|
||||
|
||||
Reference in New Issue
Block a user