Skip to content

Instantly share code, notes, and snippets.

@cybersiddhu
Created August 3, 2009 16:32
Show Gist options
  • Save cybersiddhu/160654 to your computer and use it in GitHub Desktop.
Save cybersiddhu/160654 to your computer and use it in GitHub Desktop.
package DictyREST::Renderer::TT;
use warnings;
use strict;
use Carp qw/confess cluck carp/;
use version; our $VERSION = qv('1.0.0');
# Other modules:
use base 'Mojo::Base';
use Template;
use Path::Class;
# Module implementation
#
__PACKAGE__->attr('path');
__PACKAGE__->attr('template');
__PACKAGE__->attr('option');
sub new {
my ( $class, %arg ) = @_;
my $self = {};
bless $self, $class;
foreach my $param (qw/path option/) {
$self->$param( $arg{$param} ) if defined $arg{$param};
}
return $self;
}
sub build {
my ( $self, %arg ) = @_;
my $path = $arg{path} || $self->path;
confess "template path must be set\n" if !$path;
my $dir = dir $path;
my $subdir = [ map { $_->stringify } grep { -d $_ } $dir->children ];
my $option = $self->option || '';
$option->{INCLUDE_PATH} = $subdir;
$self->template( Template->new($option) );
return sub { $self->process(@_); };
}
sub process {
my ( $self, $renderer, $c, $output ) = @_;
$c->app->log->warn("from tt template");
return 0 if $c->stash('format') ne 'html';
my $template = $c->stash('template');
my $status
= $self->template->process( $template, { %{ $c->stash }, c => $c },
$output );
if ( !$status ) {
cluck $self->template->error;
return 0;
}
return 1;
}
1; # Magic true value required at end of module
__END__
=head1 NAME
DictyREST::Renderer::TT - [Template toolkit renderer for DictyREST application]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment