1package Catalyst::View::TT::ControllerLocal; 2 3use strict; 4use base 'Catalyst::View::TT'; 5use Data::Dumper; 6 7our $VERSION = '0.02'; 8 9__PACKAGE__->mk_accessors('include_path'); 10 11 12 13 14 15=head1 NAME 16 17Catalyst::View::TT::ControllerLocal - Catalyst TT View with template 18names relative to the Controller 19 20 21 22=head1 SYNOPSIS 23 24 25=head2 Use the helper to create a View class for your 26 27 script/myapp_create.pl view TT TTControllerLocal 28 29This creates the MyApp::View::TT class. 30 31 32=head2 Forward to the View like you normally would 33 34 #Meanwhile, maybe in a private end action 35 if(!$c->res->body) { 36 if($c->stash->{template}) { 37 $c->forward('MyApp::View::TT'); 38 } else { 39 die("No output method!\n"); 40 } 41 } 42 43 44 45 46 47=head1 DESCRIPTION 48 49Catalyst::View::TT::ControllerLocal is like a normal Catalyst TT View, 50but with template file names relative to the current Controller. So 51with a set of templates like: 52 53 ./root/edit.html 54 ./root/add.html 55 ./root/Frobniz/add.html 56 57and an action C<add> in the Controller C<MyApp::Controller::Frobniz>, 58you set C<$c-E<gt>stash-E<gt>{template}> to C<add.html> in order for 59it to pick up the C<./root/frobbiz/add.html> template. 60 61Setting the C<$c-E<gt>stash-E<gt>{template}> from Controller 62C<MyApp::Controller::Bogon> would instead pick the default template in 63C<./root/add.html> (since there is no Bogon subdirectory under root). 64 65In addition, since there is no file C<edit.html> except in the Frobniz 66directory, C::V::TT::ControllerLocal will default to looking for 67C<edit.html> in ./root/ and ./root/base (or whatever you set 68MyApp->config->{INCLUDE_PATH} to). 69 70=cut 71 72 73 74=head1 METHODS 75 76=head2 new 77 78The constructor for the TT view. Sets up the template provider, and 79reads the application config. 80 81=cut 82sub new { 83 my ($class, $c, $arguments) = @_; 84 85 my $root = $c->config->{root}; 86 87 #Note: Tight coupling with the parent class: Repeat of the 88 #default and overridden values 89 my $include_path = 90 $arguments->{INCLUDE_PATH} || 91 $class->config->{INCLUDE_PATH} || 92 [ $root, "$root/base" ]; 93 $arguments->{INCLUDE_PATH} = $include_path; 94 95 my $self = $class->SUPER::new($c, $arguments); 96 $self->include_path($include_path); 97 98 return($self); 99} 100 101 102 103 104 105=head2 process 106 107Render the template specified in C<$c-E<gt>stash-E<gt>{template}> or 108C<$c-E<gt>request-E<gt>match>. 109 110The template file name is fetched from one of the Template's 111include_paths. The name of the current action's namespace is prepended 112to this list, so for the action C<edit> in 113C<MyApp::Controller::Frobniz>, the prepended directory is 114C<./root/frobniz>. 115 116Example: If C<$c-E<gt>stash-E<gt>{template}> = C<edit.html> you can put a 117specific template in ./root/myaction/edit.html, or a general template 118in ./root/base/edit.html or ./root/edit.html. 119 120If the action is MyApp::Controller::MyAction, the specific template is 121used. If the action is MyApp::Controller::MyOtherAction, the 122./root/base/edit.html is used. 123 124See also: L<Catalyst::View::TT>::process. 125 126=cut 127sub process { 128 my ($self, $c) = @_; 129 130 my $dir_action = $c->path_to('root', $c->namespace || "base"); 131 132 unshift(@{$self->include_path}, $dir_action); 133 eval { $self->SUPER::process($c); }; 134 shift(@{$self->include_path}); 135 $@ and die; 136 137 return 1; 138} 139 140 141 142 143 144=head1 AUTHOR 145 146Johan Lindstrom <johanl �T cpan.org> 147 148 149 150=head1 LICENSE 151 152This library is free software, you can redistribute it and/or modify 153it under the same terms as Perl itself. 154 155=cut 156 1571; 158