[Catalyst-commits] r11680 - in Catalyst-View-TD/trunk:
lib/Catalyst/View t t/lib t/lib/TestApp/TD
theory at dev.catalyst.perl.org
theory at dev.catalyst.perl.org
Thu Oct 29 00:12:05 GMT 2009
Author: theory
Date: 2009-10-29 00:12:04 +0000 (Thu, 29 Oct 2009)
New Revision: 11680
Added:
Catalyst-View-TD/trunk/t/06prepend_dispatchto.t
Removed:
Catalyst-View-TD/trunk/t/06includepath.t
Modified:
Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
Catalyst-View-TD/trunk/t/lib/TestApp.pm
Catalyst-View-TD/trunk/t/lib/TestApp/TD/Additional.pm
Log:
Better per-request template class overriding:
* Renamed the `add_template_classes` key to `prepend_template_classes`.
* Added the corresponding `append_template_classes` key, so that you can just
push classes on to the end of the dispatch array.
Modified: Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
===================================================================
--- Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm 2009-10-28 23:54:56 UTC (rev 11679)
+++ Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm 2009-10-29 00:12:04 UTC (rev 11680)
@@ -26,7 +26,7 @@
name => 'MyApp',
root => MyApp->path_to('root'),
'View::TD' => {
- dispatch_to => ['MyApp::View::TD::Root'], # Default
+ dispatch_to => ['TestApp::TD::HTML'], # Default
preprocessor => sub { ... },
around_template => sub { ... },
},
@@ -112,7 +112,7 @@
my $path = join '/', map {
s{(?!^)([[:upper:]])+}{_$1}g; lc
} split /::/ => $sub;
- $mod->mix($root, $path);
+ $mod->alias($root, $path);
}
return [$root];
}
@@ -157,10 +157,15 @@
my $init = $self->init;
local $self->{dispatch_to} = [
- @{ $vars->{add_template_classes} },
+ @{ $vars->{prepend_template_classes} },
@{ $self->{dispatch_to} },
- ] if ref $vars->{add_template_classes};
+ ] if ref $vars->{prepend_template_classes};
+ local $self->{dispatch_to} = [
+ @{ $self->{dispatch_to} },
+ @{ $vars->{append_template_classes} },
+ ] if ref $vars->{append_template_classes};
+
Template::Declare->init( %{ $init }, dispatch_to => $self->dispatch_to );
Template::Declare::Catalyst->context($c);
my $output = eval { Template::Declare->show($template, $vars) };
@@ -175,7 +180,6 @@
use strict;
use warnings;
-use lib '/Users/david/dev/perl/Template-Declare/mixmaster/lib';
use base qw/Template::Declare/;
__PACKAGE__->mk_classdata('context');
@@ -287,10 +291,11 @@
=head2 Dynamic C<dispatch_to>
Sometimes it is desirable to modify C<dispatch_to> for your templates at
-runtime. Additional paths can be added to the start of C<dispatch_to> via the
+runtime. Additional paths can be prepended or appeneded C<dispatch_to> via the
stash as follows:
- $c->stash->{add_template_classes} = [ 'MyApp::Other::Templates' ];
+ $c->stash->{prepend_template_classes} = [ 'MyApp::Other::Templates' ];
+ $c->stash->{append_template_classes} = [ 'MyApp::Fallback::Templates' ];
If you need to add template classes paths to the end of C<dispatch_to>, there
is also a C<include_path()> accessor:
@@ -311,11 +316,12 @@
$c->view('HTML')->dispatch_to( ['My::Templates', 'Your::Templates'] );
-This is safe to do on a per-request basis.
+This is safe to do on a per-request basis. But you're really better off using
+the stash approach. I suggest sticking to that when you can.
If you are calling C<render> directly, then you can specify extra template
-classes under the C<add_template_classes> key. See L</"Capturing Template
-Output"> for an example.
+classes under the C<prepend_template_classes> and C<append_template_classes>
+keys. See L</"Capturing Template Output"> for an example.
=head2 Rendering Views
@@ -406,8 +412,8 @@
Subject => 'A TD Email',
],
body => $c->view('TD')->render($c, 'email', {
- add_template_classes => [ 'My::EmailTemplates' ],
- email_tmpl_param1 => 'foo'
+ prepend_template_classes => [ 'My::EmailTemplates' ],
+ email_tmpl_param1 => 'foo'
}),
);
# Redirect or display a message
Deleted: Catalyst-View-TD/trunk/t/06includepath.t
===================================================================
--- Catalyst-View-TD/trunk/t/06includepath.t 2009-10-28 23:54:56 UTC (rev 11679)
+++ Catalyst-View-TD/trunk/t/06includepath.t 2009-10-29 00:12:04 UTC (rev 11680)
@@ -1,36 +0,0 @@
-use strict;
-use warnings;
-use Test::More tests => 12;
-
-use FindBin;
-use lib "$FindBin::Bin/lib";
-
-use_ok('Catalyst::Test', 'TestApp');
-my $response;
-
-my $initial_dispatch_to = [ @{ TestApp->view('Appconfig')->dispatch_to } ];
-
-ok(($response = request("/test_dispatchto?view=Appconfig&template=testclass&additionalclass=TestApp::TD::Additional"))->is_success, 'additional_template_class request');
-is($response->content, "From Additional: " . TestApp->config->{default_message}, 'additional_template_class message');
-
-is_deeply($initial_dispatch_to,
- TestApp->view('Appconfig')->dispatch_to,
- 'dispatch_to is unchanged');
-
-ok(($response = request("/test_dispatchto?view=Additional&template=testclass"))->is_success, 'dispatch_to set to the alternate class');
-is($response->content, "From Additional: " . TestApp->config->{default_message}, 'request to view using the alternate template class');
-
-ok(($response = request("/test_dispatchto?view=Appconfig&template=testclass&addclass=TestApp::TD::Additional"))->is_success, 'add class to the array');
-is($response->content, "From Additional: " . TestApp->config->{default_message}, 'added class template renders');
-
-is_deeply([@$initial_dispatch_to, 'TestApp::TD::Additional'],
- TestApp->view('Appconfig')->dispatch_to,
- 'dispatch_to has been changed');
-
-ok(($response = request("/test_dispatchto?view=Appconfig&template=testclass&setclass=TestApp::TD::Additional"))->is_success, 'set dispatch_to from request');
-is($response->content, "From Additional: " . TestApp->config->{default_message}, 'set class template renders');
-
-is_deeply(['TestApp::TD::Additional'],
- TestApp->view('Appconfig')->dispatch_to,
- 'dispatch_to has been overridden');
-
Copied: Catalyst-View-TD/trunk/t/06prepend_dispatchto.t (from rev 11678, Catalyst-View-TD/trunk/t/06includepath.t)
===================================================================
--- Catalyst-View-TD/trunk/t/06prepend_dispatchto.t (rev 0)
+++ Catalyst-View-TD/trunk/t/06prepend_dispatchto.t 2009-10-29 00:12:04 UTC (rev 11680)
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use Test::More tests => 12;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+my $response;
+
+my $initial_dispatchto = [ @{ TestApp->view('Appconfig')->dispatch_to } ];
+
+ok(($response = request("/test_prepend_dispatchto?view=Appconfig&template=testclass&additionalclass=TestApp::TD::Additional"))->is_success, 'additional_template_class request');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'additional_template_class message');
+
+is_deeply($initial_dispatchto,
+ TestApp->view('Appconfig')->dispatch_to,
+ 'dispatchto is unchanged');
+
+ok(($response = request("/test_prepend_dispatchto?view=Additional&template=testclass"))->is_success, 'dispatchto set to the alternate class');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'request to view using the alternate template class');
+
+ok(($response = request("/test_prepend_dispatchto?view=Appconfig&template=testclass&addclass=TestApp::TD::Additional"))->is_success, 'add class to the array');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'added class template renders');
+
+is_deeply([@$initial_dispatchto, 'TestApp::TD::Additional'],
+ TestApp->view('Appconfig')->dispatch_to,
+ 'dispatchto has been changed');
+
+ok(($response = request("/test_prepend_dispatchto?view=Appconfig&template=testclass&setclass=TestApp::TD::Additional"))->is_success, 'set dispatchto from request');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'set class template renders');
+
+is_deeply(['TestApp::TD::Additional'],
+ TestApp->view('Appconfig')->dispatch_to,
+ 'dispatchto has been overridden');
+
Modified: Catalyst-View-TD/trunk/t/lib/TestApp/TD/Additional.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/TD/Additional.pm 2009-10-28 23:54:56 UTC (rev 11679)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/TD/Additional.pm 2009-10-29 00:12:04 UTC (rev 11680)
@@ -10,4 +10,9 @@
outs "From Additional: $args->{message}";
};
+template test => sub {
+ my ($self, $args) = @_;
+ outs "From Additional: $args->{message}";
+};
+
1;
Modified: Catalyst-View-TD/trunk/t/lib/TestApp.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp.pm 2009-10-28 23:54:56 UTC (rev 11679)
+++ Catalyst-View-TD/trunk/t/lib/TestApp.pm 2009-10-29 00:12:04 UTC (rev 11680)
@@ -36,12 +36,12 @@
$c->stash->{message} = $c->request->param('message') || $c->config->{default_message};
}
-sub test_dispatchto : Local {
+sub test_prepend_dispatchto : Local {
my ($self, $c) = @_;
$c->stash->{message} = $c->request->param('message') || $c->config->{default_message};
$c->stash->{template} = $c->request->param('template');
if ( my $class = $c->request->param('additionalclass') ){
- $c->stash->{add_template_classes} = [$class];
+ $c->stash->{prepend_template_classes} = [$class];
}
if ( my $class = $c->request->param('addclass') ){
my $view = $self->view($c->request->param('view') || $c->config->{default_view});
@@ -53,6 +53,23 @@
}
}
+sub test_append_dispatchto : Local {
+ my ($self, $c) = @_;
+ $c->stash->{message} = $c->request->param('message') || $c->config->{default_message};
+ $c->stash->{template} = $c->request->param('template');
+ if ( my $class = $c->request->param('additionalclass') ){
+ $c->stash->{append_template_classes} = [$class];
+ }
+ if ( my $class = $c->request->param('addclass') ){
+ my $view = $self->view($c->request->param('view') || $c->config->{default_view});
+ push @{ $view->dispatch_to}, $class;
+ }
+ if ( my $class = $c->request->param('setclass') ){
+ my $view = $self->view($c->request->param('view') || $c->config->{default_view});
+ $view->dispatch_to([$class]);
+ }
+}
+
sub test_render : Local {
my ($self, $c) = @_;
More information about the Catalyst-commits
mailing list