From: Nilson Santos Figueiredo JĂșnior Date: Fri, 6 Feb 2009 19:49:59 +0000 (+0000) Subject: Add subinclude_using, bump version to 0.03 X-Git-Tag: 0.07_01~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-View-Component-SubInclude.git;a=commitdiff_plain;h=be2a019af2b1ffb2672e77b65e3644ee6ee278cd Add subinclude_using, bump version to 0.03 --- diff --git a/lib/Catalyst/View/Component/SubInclude.pm b/lib/Catalyst/View/Component/SubInclude.pm index 77c5945..20909a6 100644 --- a/lib/Catalyst/View/Component/SubInclude.pm +++ b/lib/Catalyst/View/Component/SubInclude.pm @@ -10,11 +10,11 @@ Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views =head1 VERSION -Version 0.02 +Version 0.03 =cut -our $VERSION = '0.02'; +our $VERSION = '0.03'; =head1 SYNOPSIS @@ -29,6 +29,7 @@ our $VERSION = '0.02'; Then, somewhere in your templates: [% subinclude('/my/widget') %] + [% subinclude_using('SubRequest', '/page/footer') %] =head1 DESCRIPTION @@ -45,7 +46,7 @@ ESI (L) or any other sub-include plugin you might want to implement. An LWP plugin seems useful and might be developed in the future. -=head1 STASH FUNCTION +=head1 STASH FUNCTIONS This component does its magic by exporting a C coderef entry to the stash. This way, it's easily accessible by the templates (which is the most @@ -54,7 +55,15 @@ common use-case). =head2 C This will render and return the body of the included resource (as specified by -C<$path>). +C<$path>) using the default subinclude plugin. + +=head2 C + +This will render and return the body of the included resource (as specified by +C<$path>) using the specified subinclude plugin. + +The C function above is implemented basically as a shortcut which +calls this function using the default plugin as the first parameter. =head1 SUBINCLUDE PLUGINS @@ -63,7 +72,7 @@ L, L and L. -By default, the SubRequest plugin will be used. This can be changed in the +By default, the C plugin will be used. This can be changed in the view's configuration options (either in the config file or in the view module itself). @@ -73,6 +82,14 @@ Configuration file example: subinclude_plugin ESI +=head2 C + +This method changes the current active subinclude plugin in runtime. It expects +the plugin suffix (e.g. C or C) or a fully-qualified class +name in the C namespace. + +=head2 Writing plugins + If writing your own plugin, keep in kind plugins are required to implement a class method C with the following signature: @@ -80,12 +97,15 @@ class method C with the following signature: my ($class, $c, @args) = @_; } +The default plugin is stored in the C which can be changed +in runtime. It expects a fully qualified class name. + =cut has 'subinclude_plugin' => ( is => 'rw', isa => 'ClassName' -); +); around 'new' => sub { my $next = shift; @@ -94,12 +114,7 @@ around 'new' => sub { my $self = $class->$next( @_ ); my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest'; - my $subinclude_class = __PACKAGE__ . '::' . $subinclude_plugin; - - eval "require $subinclude_class"; - croak "Error requiring $subinclude_class: $@" if $@; - - $self->subinclude_plugin( $subinclude_class ); + $self->set_subinclude_plugin( $subinclude_plugin ); $self; }; @@ -108,13 +123,45 @@ around 'render' => sub { my $next = shift; my ($self, $c, @args) = @_; - $c->stash->{subinclude} = sub { - $self->subinclude_plugin->generate_subinclude( $c, @_ ); - }; + $c->stash->{subinclude} = sub { $self->_subinclude( $c, @_ ) }; + $c->stash->{subinclude_using} = sub { $self->_subinclude_using( $c, @_ ) }; $self->$next( $c, @args ); }; +sub set_subinclude_plugin { + my ($self, $plugin) = @_; + + my $subinclude_class = $self->_subinclude_plugin_class_name( $plugin ); + $self->subinclude_plugin( $subinclude_class ); +} + +sub _subinclude { + my ($self, $c, @args) = @_; + $self->_subinclude_using( $c, $self->subinclude_plugin, @args ); +} + +sub _subinclude_using { + my ($self, $c, $plugin, @args) = @_; + $plugin = $self->_subinclude_plugin_class_name($plugin); + $plugin->generate_subinclude( $c, @args ); +} + +sub _subinclude_plugin_class_name { + my ($self, $plugin) = @_; + + # check if name is already fully qualified + my $pkg = __PACKAGE__; + return $plugin if $plugin =~ /^$pkg/; + + my $class_name = __PACKAGE__ . '::' . $plugin; + + eval "require $class_name"; + croak "Error requiring $class_name: $@" if $@; + + $class_name; +} + =head1 SEE ALSO L, diff --git a/lib/Catalyst/View/Component/SubInclude/ESI.pm b/lib/Catalyst/View/Component/SubInclude/ESI.pm index 635d978..7dfc7b3 100644 --- a/lib/Catalyst/View/Component/SubInclude/ESI.pm +++ b/lib/Catalyst/View/Component/SubInclude/ESI.pm @@ -8,11 +8,11 @@ Catalyst::View::Component::SubInclude::ESI - Edge Side Includes (ESI) plugin for =head1 VERSION -Version 0.02 +Version 0.03 =cut -our $VERSION = '0.02'; +our $VERSION = '0.03'; =head1 SYNOPSIS diff --git a/lib/Catalyst/View/Component/SubInclude/SubRequest.pm b/lib/Catalyst/View/Component/SubInclude/SubRequest.pm index baedb69..7ba7828 100644 --- a/lib/Catalyst/View/Component/SubInclude/SubRequest.pm +++ b/lib/Catalyst/View/Component/SubInclude/SubRequest.pm @@ -11,11 +11,11 @@ Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V =head1 VERSION -Version 0.02 +Version 0.03 =cut -our $VERSION = '0.02'; +our $VERSION = '0.03'; =head1 SYNOPSIS diff --git a/lib/Catalyst/View/Component/SubInclude/Visit.pm b/lib/Catalyst/View/Component/SubInclude/Visit.pm index bc7fe01..c82fe73 100644 --- a/lib/Catalyst/View/Component/SubInclude/Visit.pm +++ b/lib/Catalyst/View/Component/SubInclude/Visit.pm @@ -11,11 +11,11 @@ Catalyst::View::Component::SubInclude::Visit - visit() plugin for C::V::Componen =head1 VERSION -Version 0.02 +Version 0.03 =cut -our $VERSION = '0.02'; +our $VERSION = '0.03'; =head1 SYNOPSIS diff --git a/t/ESITest/root/index.tt b/t/ESITest/root/index.tt index 2bab3a9..0c96357 100644 --- a/t/ESITest/root/index.tt +++ b/t/ESITest/root/index.tt @@ -1,3 +1,9 @@ -ESI Test, will include /time_include
+SubInclude test: will include /time_include using default plugin [% c.view('TT').subinclude_plugin %]
[% subinclude('/time_include', { 'foo' => 'bar'} ) %]
[% subinclude('/time_args', ['test'], { 'baz' => 'quux' }) %]
+ +

+Test subinclude using specific plugins:
+[% subinclude_using('SubRequest', '/time_include', { 'plugin' => 'SubRequest'} ) %]
+[% subinclude_using('Visit', '/time_include', { 'plugin' => 'Visit'} ) %]
+[% subinclude_using('ESI', '/time_include', { 'plugin' => 'ESI'} ) %]