[Catalyst] Context sensitive template locations
Johan Lindström
johanl at DarSerMan.com
Mon Nov 21 19:20:23 CET 2005
At 01:46 2005-11-21, phaylon wrote:
>will be called on /foobar/test, which creates a $c->req->match of
>"foobar/test".
Not quite what I was looking for, but in the general direction.
This is what I ended up with. I subclassed my TT view and overloaded:
-----
=item process
Renders the template specified in C<$c-E<gt>stash-E<gt>{template}> or
C<$c-E<gt>request-E<gt>match>.
The template file name is fetched from one of the Template's
include_paths. The name of the current action's namespace is prepended
to this list, so for the action C<edit> in
C<MyApp::Controller::Frobniz>, the prepended directory is
C<./root/frobniz>.
Example: If C<$c-E<gt>stash-E<gt>{template}> = C<edit.html> you can put a
specific template in ./root/myaction/edit.html, or a general template
in ./root/base/edit.html or ./root/edit.html.
If the action is MyApp::Controller::MyAction, the specific template is
used. If the action is MyApp::Controller::MyOtherAction, the
./root/base/edit.html is used.
Template variables are set up from the contents of C<$c-E<gt>stash>,
augmented with C<base> set to C<$c-E<gt>req-E<gt>base>, C<c> to C<$c>
and C<name> to C<$c-E<gt>config-E<gt>{name}>.
Alternately, the C<CATALYST_VAR> configuration item can be defined to
specify the name of a template variable through which the context
reference (C<$c>) can be accessed. In this case, the C<c>, C<base>
and C<name> variables are omitted.
Output is stored in C<$c-E<gt>response-E<gt>output>.
=cut
sub process {
my ( $self, $c ) = @_;
my $dir_action = $c->path_to('root', $c->namespace || "base");
local
$self->template->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->{INCLUDE_PATH}
= [
$dir_action,
@{$self->template->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->{INCLUDE_PATH}},
];
$self->SUPER::process($c);
return 1;
}
-----
So a few questions.
1) I felt this was important after looking at the code duplication in the
templates created by the scaffolding helper. but does this sound useful to
you? Would you use it?
2) Does that TT thing look right, gutting the internals like that? I
searched the TT list archives and it sounded like it was something like
this that was recommended, but I couldn't find any code samples to do this.
3) Should this be folded into Catalyst::View::TT? As default behaviour, or
with a config switch? If not, I think I'll release this as
Catalyst::View::TT::ActionSensitive or CVTT::ActionRelative or something
like that. Name suggestions?
/J
More information about the Catalyst
mailing list