diff --git a/.gitignore b/.gitignore index 3780509..5de1957 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ pm_to_blib* .lwpcookies MojoX-Renderer-TT-* cover_db +*~ diff --git a/Changes b/Changes index 58ec8fe..77b0cd8 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,5 @@ Revision history for MojoX-Renderer-TT -0.01 Date/time +0.01 November 9, 2008 First version, released on an unsuspecting world. diff --git a/Makefile.PL b/Makefile.PL index 5fc54b0..2038b52 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,6 +13,7 @@ WriteMakefile( PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, + 'Mojo' => 0.8008, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'MojoX-Renderer-TT-*' }, diff --git a/README b/README index 40883e5..0b453a7 100644 --- a/README +++ b/README @@ -1,16 +1,7 @@ MojoX-Renderer-TT -The README is used to introduce the module and provide instructions on -how to install the module, any machine dependencies it may have (for -example C compilers and installed libraries) and any other information -that should be provided before the module is installed. - -A README file is required for CPAN modules since CPAN extracts the README -file from a module distribution so that people browsing the archive -can use it to get an idea of the module's uses. It is usually a good idea -to provide version information here so that people can decide whether -fixes for the module are worth downloading. - +This module implements a Template Toolkit renderer for the Mojo +framework. INSTALLATION @@ -33,9 +24,6 @@ You can also look for information at: RT, CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=MojoX-Renderer-TT - AnnoCPAN, Annotated CPAN documentation - http://annocpan.org/dist/MojoX-Renderer-TT - CPAN Ratings http://cpanratings.perl.org/d/MojoX-Renderer-TT diff --git a/lib/MojoX/Renderer/TT.pm b/lib/MojoX/Renderer/TT.pm index 77c7731..7cd2335 100644 --- a/lib/MojoX/Renderer/TT.pm +++ b/lib/MojoX/Renderer/TT.pm @@ -2,10 +2,75 @@ package MojoX::Renderer::TT; use warnings; use strict; +use base 'Mojo::Base'; + +use Template (); +use Carp (); + +__PACKAGE__->attr('tt', + chained => 1, +); + + +sub new { + my $self = shift->SUPER::new(@_); + $self->_init(@_); + return sub { $self->_render(@_) } +} + +sub _init { + my $self = shift; + my %args = @_; + + my $mojo = delete $args{mojo}; + + my $dir = $mojo && $mojo->home->rel_dir('tmp/ctpl'); + + # TODO + # take and process options :-) + + my %config = ( + COMPILE_EXT => '.ttc', + COMPILE_DIR => ($dir || "/tmp"), + UNICODE => 1, + ENCODING => 'utf-8', + CACHE_SIZE => 128, + RELATIVE => 1, + ABSOLUTE => 1, + ); + + $self->tt(Template->new( \%config )) + or Carp::croak "Could not initialize Template object: $Template::ERROR"; + + return $self; +} + +sub _render { + my ($self, $c, $tx, $path) = @_; + + #use Data::Dump qw(dump); + #warn dump(\@args); + + my $output; + unless ( $self->tt->process( $path, + { c => $c, + tx => $tx, + }, + \$output, + { binmode => ":utf8" } + ) + ) { + Carp::carp $self->tt->error . "\n"; + return $self->tt->error; + } + else { + return $output; + } +} =head1 NAME -MojoX::Renderer::TT - The great new MojoX::Renderer::TT! +MojoX::Renderer::TT - Template Toolkit renderer for Mojo =head1 VERSION @@ -18,35 +83,20 @@ our $VERSION = '0.01'; =head1 SYNOPSIS -Quick summary of what the module does. - Perhaps a little code snippet. use MojoX::Renderer::TT; - my $foo = MojoX::Renderer::TT->new(); - ... + sub startup { + ... + $renderer->add_handler( tt => MojoX::Renderer::TT->new( mojo => $self ) ); + } -=head1 EXPORT -A list of functions that can be exported. You can delete this section -if you don't export anything, such as for a purely object-oriented module. +=head1 METHODS -=head1 FUNCTIONS +=head2 new -=head2 function1 - -=cut - -sub function1 { -} - -=head2 function2 - -=cut - -sub function2 { -} =head1 AUTHOR