From: Aaron Trevena Date: Tue, 7 Feb 2006 21:56:22 +0000 (+0000) Subject: reworked components X-Git-Tag: 2.11~61 X-Git-Url: https://git.decadent.org.uk/gitweb/?p=maypole.git;a=commitdiff_plain;h=ba248b38837f2089582aa4a5e0faa3f2801e6c5a reworked components git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@457 48953598-375a-da11-a14b-00016c27c3ee --- diff --git a/MANIFEST b/MANIFEST index e744e36..a578fbb 100644 --- a/MANIFEST +++ b/MANIFEST @@ -11,7 +11,6 @@ lib/Maypole/Config.pm lib/Maypole/Constants.pm lib/Maypole/Headers.pm lib/Maypole/Session.pm -lib/Maypole/Components.pm lib/Maypole/Manual.pod lib/Maypole/Manual/About.pod lib/Maypole/Manual/Install.pod diff --git a/lib/Maypole.pm b/lib/Maypole.pm index 5db199b..c43de77 100644 --- a/lib/Maypole.pm +++ b/lib/Maypole.pm @@ -8,6 +8,8 @@ use Maypole::Constants; use Maypole::Headers; use Maypole::Components; use URI(); +use URI::QueryParam; +use NEXT; use File::MMagic::XS qw(:compat); our $VERSION = '2.11'; @@ -202,7 +204,7 @@ __PACKAGE__->mk_classdata($_) for qw( config init_done view_object model_classes __PACKAGE__->mk_accessors( qw( params query objects model_class template_args output path args action template error document_encoding content_type table - headers_in headers_out stash status) + headers_in headers_out stash status parent) ); __PACKAGE__->config( Maypole::Config->new() ); @@ -317,12 +319,11 @@ sub setup_model { # among other things, this populates $config->classes $config->model->setup_database($config, $class, @_); - + foreach my $subclass ( @{ $config->classes } ) { next if $subclass->isa("Maypole::Model::Base"); no strict 'refs'; - unshift @{ $subclass . "::ISA" }, $config->model; - + unshift @{ $subclass . "::ISA" }, $config->model; } # Load custom model code, if it exists - nb this must happen after the @@ -467,12 +468,48 @@ sub handler : method { return $status; } +=back + +=head2 component + + Run Maypole sub-requests as components using L + + [% request.component("/beer/view_as_component/20") %] + + Allows you to integrate the results of a Maypole request into an existing +request. You'll need to set up actions and templates +which return fragments of HTML rather than entire pages, but once you've +done that, you can use the C method of the Maypole request object +to call those actions. You may pass a query string in the usual URL style. +You should not fully qualify the Maypole URLs. + +=cut + sub component { - my ($r,$path) = @_; - my $component = Maypole::Components->new(@_); - return $component->handler($path); + my ( $r, $path ) = @_; + my $self = bless { parent => $r }, ref $r; + my $url = URI->new($path); + $self->{path} = $url->path; + $self->parse_path; + $self->params( $url->query_form_hash ); + $self->query( $r->params ); + $self->handler_guts; + return $self->output; } +sub get_template_root { + my $self = shift; + my $r = shift; + return $r->parent->get_template_root if $r->{parent}; + return $self->NEXT::DISTINCT::get_template_root( $r, @_ ); +} + +sub view_object { + my $self = shift; + my $r = shift; + return $r->parent->view_object if $r->{parent}; + return $self->NEXT::DISTINCT::view_object( $r, @_ ); +} # Instead of making plugin authors use the NEXT::DISTINCT hoopla to ensure other # plugins also get to call the hook, we can cycle through the application's @@ -1080,8 +1117,6 @@ backend. Otherwise, see L =cut -sub get_template_root {'.'} - =back =head2 Request properties diff --git a/lib/Maypole/Components.pm b/lib/Maypole/Components.pm deleted file mode 100644 index 31d6ac2..0000000 --- a/lib/Maypole/Components.pm +++ /dev/null @@ -1,65 +0,0 @@ -package Maypole::Components; -use strict; -use warnings; -use URI; -use URI::QueryParam; - -our @ISA = qw(Maypole); - -sub new { - my ($class,$r) = @_; - my $self = bless { config => $r->config, parent => $r }, $class; -} - -sub handler { - my $self = shift; - my $path = shift; - my $url = URI->new($path); - $self->{path} = $url->path; - $self->parse_path; - $self->{query} = $url->query_form_hash; - $self->handler_guts; - return $self->{output}; -} - -sub get_template_root { shift->{parent}->get_template_root } -sub view_object { shift->{parent}->view_object } - -1; -__END__ - -=head1 NAME - -Maypole::Components - Run Maypole sub-requests as components - -=head1 SYNOPSIS - - package BeerDB; - use base qw(Maypole); - - - - [% request.component("/beer/view_as_component/20") %] - -=head1 DESCRIPTION - -This subclass of Maypole allows you to integrate the results of a Maypole -request into an existing request. You'll need to set up actions and templates -which return fragments of HTML rather than entire pages, but once you've -done that, you can use the C method of the Maypole request object -to call those actions. You may pass a query string in the usual URL style. -You should not fully qualify the Maypole URLs. - -=head1 SEE ALSO - -http://maypole.perl.org/ - -=head1 AUTHOR - -Simon Cozens, Esimon@cpan.orgE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2004 by Simon Cozens - -=cut