/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

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