PageRenderTime 42ms CodeModel.GetById 6ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Render/WithTT.pm

http://github.com/gshank/html-formhandler
Perl | 121 lines | 94 code | 25 blank | 2 comment | 2 complexity | 560e702ac4ba208d1ba605275cf5cf0d MD5 | raw file
  1package HTML::FormHandler::Render::WithTT;
  2# ABSTRACT: tt rendering
  3
  4use Moose::Role;
  5use File::ShareDir;
  6use Template;
  7use namespace::autoclean;
  8use HTML::FormHandler::Render::Util ('process_attrs');
  9
 10=head1 SYNOPSIS
 11
 12A rendering role for HTML::FormHandler that allows rendering using
 13Template::Toolkit
 14
 15   package MyApp::Form;
 16   use HTML::FormHandler::Moose;
 17   extends 'HTML::FormHandler';
 18   with 'HTML::FormHandler::Render::WithTT';
 19
 20   sub build_tt_template { 'user_form.tt' }
 21   sub build_tt_include_path { ['root/templates'] }
 22   ....< define form >....
 23
 24   my $form = MyApp::Form->new(
 25   $form->tt_render;
 26
 27If you want to render with TT, you don't need this role. Just use
 28one of the TT form templates provided, form.tt or form_in_one.tt.
 29If you use this role to render, you are using two different TT
 30engines, with different sets of variables, etc, which doesn't
 31make much sense.
 32
 33This is mainly useful as a testing aid and an example of using the
 34sample templates.
 35
 36=head1 DESCRIPTION
 37
 38Uses 'tt_render' instead of 'render' to allow using both TT templates and the
 39built-in rendering.
 40
 41=cut
 42
 43has 'tt_include_path' => (
 44    traits => ['Array'],
 45    is => 'rw',
 46    isa => 'ArrayRef',
 47    lazy => 1,
 48    builder => 'build_tt_include_path',
 49    handles => {
 50       add_tt_include_path => 'push',
 51    }
 52);
 53sub build_tt_include_path {[]}
 54
 55has 'tt_config' => (
 56    traits => ['Hash'],
 57    is => 'rw',
 58    lazy => 1,
 59    builder => 'build_tt_config',
 60);
 61sub build_tt_config {
 62    my $self = shift;
 63    return {
 64        INCLUDE_PATH => [
 65           @{ $self->tt_include_path },
 66           File::ShareDir::dist_dir('HTML-FormHandler') . '/templates/'
 67        ]
 68    };
 69}
 70
 71# either file name string or string ref?
 72has 'tt_template' => ( is => 'rw', isa => 'Str', lazy => 1,
 73   builder => 'build_tt_template' );
 74sub build_tt_template { 'form/form.tt' }
 75
 76has 'tt_engine' => ( is => 'rw', isa => 'Template', lazy => 1,
 77   builder => 'build_tt_engine'
 78);
 79sub build_tt_engine {
 80    my $self = shift;
 81
 82    my $tt_engine = Template->new( $self->tt_config );
 83    return $tt_engine;
 84}
 85
 86has 'tt_vars' => ( is => 'rw', traits => ['Hash'],
 87    builder => 'build_tt_vars');
 88sub build_tt_vars {{}}
 89
 90has 'default_tt_vars' => ( is => 'ro', isa => 'HashRef',
 91   lazy => 1, builder => 'build_default_tt_vars' );
 92sub build_default_tt_vars {
 93    my $self = shift;
 94    return { form => $self->form, process_attrs => \&process_attrs };
 95}
 96
 97has 'tt_default_options' => (
 98    traits => ['Hash'],
 99    is => 'rw',
100    isa => 'HashRef',
101    lazy => 1,
102    builder => 'build_tt_default_options',
103);
104sub build_tt_default_options {{}}
105
106
107sub tt_render {
108    my $self = shift;
109
110    my $output;
111    my $vars = { %{$self->default_tt_vars}, %{$self->tt_vars} };
112    $self->tt_engine->process( $self->tt_template, $vars, \$output );
113
114    if( my $exception = $self->tt_engine->{SERVICE}->{_ERROR} ) {
115
116        die $exception->[0] . " " . $exception->[1] . ".  So far => " . ${$exception->[2]} . "\n";
117    }
118    return $output;
119}
120
1211;