From c71ae883fb471ca822e056374091f6b5f5a08363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sun, 9 Nov 2008 19:25:53 -0800 Subject: [PATCH] Pass parameters from the render() method to the template Require Mojo 0.8009 (for the options/parameter passing) Update documentation --- Makefile.PL | 2 +- lib/MojoX/Renderer/TT.pm | 41 ++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 2038b52..b0b6993 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,7 +13,7 @@ WriteMakefile( PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, - 'Mojo' => 0.8008, + 'Mojo' => 0.8009, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'MojoX-Renderer-TT-*' }, diff --git a/lib/MojoX/Renderer/TT.pm b/lib/MojoX/Renderer/TT.pm index 1f074e2..a99c382 100644 --- a/lib/MojoX/Renderer/TT.pm +++ b/lib/MojoX/Renderer/TT.pm @@ -9,7 +9,6 @@ use Carp (); __PACKAGE__->attr('tt', chained => 1,); - sub new { my $self = shift->SUPER::new(@_); $self->_init(@_); @@ -44,23 +43,15 @@ sub _init { } sub _render { - my ($self, $c, $tx, $path) = @_; + my ($self, $mojo, $tx, $path, $args) = @_; + + $args ||= {}; #use Data::Dump qw(dump); - #warn dump(\@args); + #warn dump(\$args); my $output; - unless ( - $self->tt->process( - $path, - { c => $c, - tx => $tx, - }, - \$output, - {binmode => ":utf8"} - ) - ) - { + unless ($self->tt->process($path, {%$args, tx => $tx}, \$output, {binmode => ":utf8"})) { Carp::carp $self->tt->error . "\n"; return $self->tt->error; } @@ -84,25 +75,43 @@ our $VERSION = '0.01'; =head1 SYNOPSIS -Perhaps a little code snippet. +Add the handler: use MojoX::Renderer::TT; sub startup { ... - $renderer->add_handler( tt => MojoX::Renderer::TT->new( mojo => $self ) ); + $self->renderer->add_handler( tt => MojoX::Renderer::TT->new( mojo => $self ) ); } +And then in the handler call render which will call the +MojoX::Renderer::TT renderer. + + $c->render(foo => 123, bar => [qw(giz mo)]); + =head1 METHODS =head2 new +This currently requires a C parameter pointing to the base class (Mojo). +object. This method returns not a TT object, but a handler for the Mojo renderer. =head1 AUTHOR Ask Bjørn Hansen, C<< >> +=head1 TODO + + * Rename C to something more sensical? + * Better support non-Mojolicious frameworks + * Don't require the mojo parameter + * Move the default template cache directory? + * Should the "tx" tpl parameter be called "c" (for context) instead? + * Accept options for setting up the Tempate object + * Better way to pass parameters to the templates? + * More sophisticated default search path? + =head1 BUGS Please report any bugs or feature requests to C, or through