[Catalyst-commits] r11706 - in Catalyst-View-TD/trunk: lib
lib/Catalyst/Helper lib/Catalyst/View lib/Template
lib/Template/Declare t
theory at dev.catalyst.perl.org
theory at dev.catalyst.perl.org
Sun Nov 1 21:38:16 GMT 2009
Author: theory
Date: 2009-11-01 21:38:15 +0000 (Sun, 01 Nov 2009)
New Revision: 11706
Added:
Catalyst-View-TD/trunk/lib/Template/
Catalyst-View-TD/trunk/lib/Template/Declare/
Catalyst-View-TD/trunk/lib/Template/Declare/Catalyst.pm
Modified:
Catalyst-View-TD/trunk/lib/Catalyst/Helper/TDClass.pm
Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
Catalyst-View-TD/trunk/t/03podspelling.t
Log:
Moved Template::Declare::Catalyst to its own file.
This makes it easier to simply use it from template classes.
Also:
* Updated TDClass to always make new template classes inherit from
Template::Declare::Catalyst.
* Updated example code to demonstrate this change.
* Various other documentation updates.
Modified: Catalyst-View-TD/trunk/lib/Catalyst/Helper/TDClass.pm
===================================================================
--- Catalyst-View-TD/trunk/lib/Catalyst/Helper/TDClass.pm 2009-11-01 21:02:10 UTC (rev 11705)
+++ Catalyst-View-TD/trunk/lib/Catalyst/Helper/TDClass.pm 2009-11-01 21:38:15 UTC (rev 11706)
@@ -71,6 +71,7 @@
use strict;
use warnings;
+use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
[% IF scaffold -%]
Modified: Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
===================================================================
--- Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm 2009-11-01 21:02:10 UTC (rev 11705)
+++ Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm 2009-11-01 21:38:15 UTC (rev 11706)
@@ -2,9 +2,9 @@
use strict;
use warnings;
+use parent qw/Catalyst::View/;
+use Template::Declare::Catalyst;
-use base qw/Catalyst::View/;
-
our $VERSION = '0.01';
__PACKAGE__->mk_accessors('init');
@@ -21,20 +21,6 @@
./script/myapp_create.pl view HTML TD
-Configure in F<lib/MyApp.pm> (could be set from config file instead):
-
- MyApp->config(
- name => 'MyApp',
- root => MyApp->path_to('root'),
- 'View::HTML => {
- dispatch_to => ['TestApp::Templates::HTML'], # Default
- strict => 1,
- auto_alias => 1,
- preprocessor => sub { ... },
- around_template => sub { ... },
- },
- );
-
Create a template by editing F<lib/MyApp/Templates/HTML.pm>:
template hello => sub {
@@ -175,16 +161,6 @@
return Template::Declare->show($template, $vars);
}
-package Template::Declare::Catalyst;
-
-use strict;
-use warnings;
-use base qw/Template::Declare/;
-
-__PACKAGE__->mk_classdata('context');
-
-*c = \&context;
-
1;
__END__
@@ -223,6 +199,7 @@
use strict;
use warnings;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
# template hello => sub {
@@ -238,8 +215,7 @@
C<end()> method, for example, to automatically forward all actions to the TD
view class.
- # In MyApp or MyApp::Controller::SomeController
-
+ # In MyApp::Controller::SomeController
sub end : Private {
my( $self, $c ) = @_;
$c->forward( $c->view('HTML') );
@@ -247,9 +223,10 @@
=head2 Configuration
-There are a three different ways to configure your view class. The first way
-is to call the C<config()> method in the view subclass. This happens when the
-module is first loaded.
+There are a three different ways to configure your view class (see
+L<config|/config> for an explanation of the configuration options). The first
+way is to call the C<config()> method in the view subclass. This happens when
+the module is first loaded.
package MyApp::View::HTML;
@@ -319,6 +296,7 @@
C<MyApp::View::XHTML> view named C<MyApp::Templates::XHTML>:
package TestApp::Templates::XHTML;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
template home => sub {
@@ -346,8 +324,8 @@
};
But it can get to be a nightmare to manage I<all> of your templates in this
-one class. A better idea is to divide them up into other classes just as you
-have them in separate controllers. The C<auto_alias> feature of
+one class. A better idea is to define them in multiple template classes just
+as you have actions in multiple controllers. The C<auto_alias> feature of
Catalyst::View::TD does just that. Rather than define a template named
C<users/list> in the dispatch class (C<MyApp::Templates::XHTML>), create a new
template class, C<MyApp::Templates::XHTML::Users>:
@@ -357,6 +335,7 @@
Then create a C<list> template there:
package TestApp::Templates::XHTML::Users;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
template list => sub {
@@ -364,17 +343,18 @@
ul { li { $_ } for @{ $args->{users} } };
};
-Catalyst::View::TD will automatically mix the templates found in all classes
-defined below the dispatch class into the dispatch class. Thus this template
-will be mixed into C<MyApp::Templates::XHTML> as C<users/list>. The nice thing
-about this is it allows you to create template classes with templates that
-correspond directly to controller classes and their actions.
+Catalyst::View::TD will automatically import the templates found in all
+classes defined below the dispatch class. Thus this template will be imported
+as C<users/list>. The nice thing about this is it allows you to create
+template classes with templates that correspond directly to controller classes
+and their actions.
You can also use this approach to create utility templates. For example,
if you wanted to put the header and footer output into utility templates,
you could put them into a utility class:
package TestApp::Templates::XHTML::Util;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
template header => sub {
@@ -393,6 +373,7 @@
other aliased template class, including the dispatch class:
package TestApp::Templates::XHTML;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
template home => sub {
@@ -408,6 +389,7 @@
And the users class:
package TestApp::Templates::XHTML::Users;
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
template list => sub {
@@ -421,13 +403,13 @@
};
};
-If you'd rather control aliasing of templates yourself, you can always set
-C<auto_alias> to a false value. Then you'd just need to explicitly inherit
+If you'd rather control the importing of templates yourself, you can always
+set C<auto_alias> to a false value. Then you'd just need to explicitly inherit
from C<Template::Declare::Catayst> and do the mixing yourself. The equivalent
to the auto-aliasing in the above examples would be:
package TestApp::Templates::XHTML;
- use base 'Template::Declare::Catalyst';
+ use parent 'Template::Declare::Catalyst';
use Template::Declare::Tags;
use TestApp::Templates::XHTML::Users;
@@ -448,15 +430,15 @@
$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
+If you need to munge the list of dispatch classes in more complex ways, there
is also a C<dispatch_to()> accessor:
my $view = $c->view('HTML')
- push @{ $view->dispatch_to }, 'My::Templates'
+ splice @{ $view->dispatch_to }, 1, 0, 'My::Templates'
unless grep { $_ eq 'My::Templates' } $view->dispatch_to;
-Note that if you use C<dispatch_to()> to add extra template classes, they are
-I<permanently> added. You therefore B<must> check for duplicate paths if you
+Note that if you use C<dispatch_to()> to change template classes, they are
+I<permanently> changed. You therefore B<must> check for duplicate paths if you
do this on a per-request basis, as in this example. Otherwise, the class will
continue to be added on every request, which would be a rather ugly memory
leak.
@@ -508,9 +490,9 @@
Template classes are automatically subclasses of Template::Declare::Catalyst,
which is itself a subclass of L<Template::Declare|Template::Declare>.
-Template::Declare::Catalyst provides a few extra accessors for use in your
-templates (though note that they will return C<undef> if you call C<render()>
-without a context object):
+L<Template::Declare::Catalyst|Template::Declare::Catalyst> provides a few
+extra accessors for use in your templates (though note that they will return
+C<undef> if you call C<render()> without a context object):
=over
@@ -615,7 +597,8 @@
=item strict
-Set to true by default (it's false by default in Template::Declare).
+Set to true by default so that exceptional conditions are appropriately fatal
+(it's false by default in Template::Declare).
=back
Added: Catalyst-View-TD/trunk/lib/Template/Declare/Catalyst.pm
===================================================================
--- Catalyst-View-TD/trunk/lib/Template/Declare/Catalyst.pm (rev 0)
+++ Catalyst-View-TD/trunk/lib/Template/Declare/Catalyst.pm 2009-11-01 21:38:15 UTC (rev 11706)
@@ -0,0 +1,67 @@
+package Template::Declare::Catalyst;
+
+use strict;
+use warnings;
+use parent 'Template::Declare';
+
+__PACKAGE__->mk_classdata('context');
+
+*c = \&context;
+
+1;
+__END__
+
+=head1 Name
+
+Template::Declare::Catalyst - Template::Declare subclass for Catalyst
+
+=head1 Synopsis
+
+ use parent 'Template::Declare::Catalyst';
+ use Template::Declare::Tags;
+
+ template hello => sub {
+ my ($self, $vars) = @_;
+ html {
+ head { title { "Hello, $vars->{user}" } };
+ body { h1 { "Hello, $vars->{user}" } };
+ };
+ };
+
+=head1 Description
+
+This subclass of L<Template::Declare|Template::Declare> adds extra
+functionality for use in L<Catalyst|Catalyst> with
+L<Catalyst::View::TD|Catalyst::View::TD>.
+
+=head1 Interface
+
+=head2 Class Methods
+
+=head3 C<context>
+
+ my $c = Template::Declare::Catalyst->context;
+
+Returns the Catalyst context object, if available.
+
+=head3 C<c>
+
+ my $c = Template::Declare::Catalyst->c;
+
+An alias for C<context>.
+
+=head1 SEE ALSO
+
+L<Catalyst::View::TD>, L<Catalyst::Helper::View::TD>,
+L<Catalyst::Helper::TDClass>.
+
+=head1 Author
+
+David E. Wheeler <david at justatheory.com>
+
+=head1 License
+
+This library is free software. You can redistribute it and/or modify it under
+the same terms as perl itself.
+
+=cut
Modified: Catalyst-View-TD/trunk/t/03podspelling.t
===================================================================
--- Catalyst-View-TD/trunk/t/03podspelling.t 2009-11-01 21:02:10 UTC (rev 11705)
+++ Catalyst-View-TD/trunk/t/03podspelling.t 2009-11-01 21:38:15 UTC (rev 11706)
@@ -13,3 +13,4 @@
namespace
tclass
vclass
+munge
More information about the Catalyst-commits
mailing list