Pass parameters from the render() method to the template

Require Mojo 0.8009 (for the options/parameter passing)

Update documentation
This commit is contained in:
Ask Bjørn Hansen
2008-11-09 19:25:53 -08:00
parent 79f7aa77f2
commit c71ae883fb
2 changed files with 26 additions and 17 deletions

View File

@@ -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-*' },

View File

@@ -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<mojo> 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<< <ask at develooper.com> >>
=head1 TODO
* Rename C<new> 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<bug-mojox-renderer-tt at rt.cpan.org>, or through