package Catalyst::View::Spreadsheet::Template; use Moose; use namespace::autoclean; # ABSTRACT: render Spreadsheet::Template templates in Catalyst use Path::Class; use Try::Tiny; use Spreadsheet::Template; extends 'Catalyst::View'; =head1 SYNOPSIS package MyApp::View::Spreadsheet::Template; use Moose; extends 'Catalyst::View::Spreadsheet::Template'; =head1 DESCRIPTION This module provides a L for L. =cut =attr path Template search path. Defaults to C<< [ $c->path_to('root') ] >>. =cut has path => ( traits => ['Array'], isa => 'ArrayRef[Path::Class::Dir]', writer => 'set_path', predicate => 'has_path', handles => { path => 'elements', }, ); =attr processor_class The C to pass through to the L object. =cut has processor_class => ( is => 'ro', isa => 'Str', default => 'Spreadsheet::Template::Processor::Xslate', ); =attr writer_class The C to pass through to the L object. =cut has writer_class => ( is => 'ro', isa => 'Str', default => 'Spreadsheet::Template::Writer::XLSX', ); =attr template_extension The extension to use for template files. Defaults to C. =cut has template_extension => ( is => 'ro', isa => 'Str', default => 'json', ); =attr catalyst_var The variable name to use for the Catalyst context object in the template. Defaults to C. =cut has catalyst_var => ( is => 'ro', isa => 'Str', default => 'c', ); has renderer => ( is => 'rw', isa => 'Spreadsheet::Template', ); sub ACCEPT_CONTEXT { my $self = shift; my ($c) = @_; $self->renderer( Spreadsheet::Template->new( processor_class => $self->processor_class, writer_class => $self->writer_class, ) ); $self->set_path([ $c->path_to('root') ]) unless $self->has_path; return $self; } my %content_types = ( xlsx => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', xls => 'application/vnd.ms-excel', ); sub process { my $self = shift; my ($c) = @_; return try { my $content = $self->render($c); $c->response->content_type($content_types{$self->_extension}); $c->response->body($content); 1; } catch { my $error = "Couldn't render template: $_"; $c->log->error($error); $c->error($error); 0; }; } sub render { my $self = shift; my ($c) = @_; $self->renderer->render( scalar($self->template_file($c)->slurp(iomode => '<:encoding(UTF-8)')), { %{ $c->stash }, $self->catalyst_var => $c, } ); } sub template_file { my $self = shift; my ($c) = @_; my $file = $c->stash->{template} || $c->action . '.' . $self->template_extension; for my $dir ($self->path) { my $full_path = $dir->file($file); if (-e $full_path) { return $full_path; } } die "Couldn't find template file $file in " . join(", ", $self->path); } sub _extension { my $self = shift; (my $extension = lc($self->writer_class)) =~ s/.*:://; return $extension; } __PACKAGE__->meta->make_immutable; no Moose; =head1 BUGS No known bugs. Please report any bugs to GitHub Issues at L. =head1 SEE ALSO L L =head1 SUPPORT You can find this documentation for this module with the perldoc command. perldoc Catalyst::View::Spreadsheet::Template You can also look for information at: =over 4 =item * MetaCPAN L =item * RT: CPAN's request tracker L =item * Github L =item * CPAN Ratings L =back =head1 SPONSORS Parts of this code were paid for by =over 4 =item Socialflow L =back =begin Pod::Coverage render template_file =end Pod::Coverage =cut 1;