Make the subrequest plugins Moose classes with a constructor
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude / SubRequest.pm
CommitLineData
30726632 1package Catalyst::View::Component::SubInclude::SubRequest;
8bbd65bf 2use Moose;
11a93ea1 3use Carp qw/croak/;
8bbd65bf 4use namespace::clean -except => 'meta';
11a93ea1 5
4e327756 6=head1 NAME
7
8Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V::Component::SubInclude
9
10=head1 VERSION
11
9399de67 12Version 0.07
4e327756 13
14=cut
15
9399de67 16our $VERSION = '0.07';
4e327756 17
18=head1 SYNOPSIS
19
20In your application class:
21
22 package MyApp;
23
24 use Catalyst qw/
25 ConfigLoader
26 Static::Simple
27 ...
28 SubRequest
29 /;
30
31In your view class:
32
33 package MyApp::View::TT;
34 use Moose;
35
36 extends 'Catalyst::View::TT';
37 with 'Catalyst::View::Component::SubInclude';
38
39 __PACKAGE__->config( subinclude_plugin => 'SubRequest' );
40
41Then, somewhere in your templates:
42
43 [% subinclude('/my/widget') %]
44
45=head1 DESCRIPTION
46
47C<Catalyst::View::Component::SubInclude::SubRequest> uses Catalyst sub-requests
48to render the subinclude contents.
49
50It requires L<Catalyst::Plugin::SubRequest>.
51
11a93ea1 52=head1 CLASS METHODS
4e327756 53
11a93ea1 54=head2 C<generate_subinclude( $c, $path, @args )>
4e327756 55
532f3bcf 56This will make a sub-request call to the action specified by C<$path>. Note that
57C<$path> should be the private action path - translation to the public path is
58handled internally.
4e327756 59
532f3bcf 60So, after path translation, the call will be (roughly) equivalent to:
61
62 $c->sub_request( $translated_path, {}, @args );
4e327756 63
64Notice that the stash will always be empty. This behavior could be configurable
65in the future through an additional switch - for now, this behavior guarantees a
532f3bcf 66common interface for all plugins.
4e327756 67
68=cut
69
30726632 70sub generate_subinclude {
7094e990 71 my ($class, $config, $c, $path, @params) = @_;
72 my $stash = $config->{keep_stash} ? { %{ $c->stash } } : {};
30726632 73
74 croak "subincludes through subrequests require Catalyst::Plugin::SubRequest"
75 unless $c->can('sub_request');
3c5cb6d6 76
c2d8d5b5 77 my $query = ref $params[-1] eq 'HASH' ? pop @params : {};
e88af283 78
c2d8d5b5 79 my $action = blessed($path)
80 ? $path
81 : $c->dispatcher->get_action_by_path($path);
30726632 82
c2d8d5b5 83 my $uri = $c->uri_for( $action, @params );
e88af283 84
9c2c47b0 85 $c->sub_request( $uri->path, $stash, $query );
30726632 86}
87
4e327756 88=head1 SEE ALSO
89
90L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>,
91L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>
92
93=head1 AUTHOR
94
95Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
96
97=head1 SPONSORSHIP
98
99Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
100
101=head1 COPYRIGHT & LICENSE
102
103Copyright (C) 2009 Nilson Santos Figueiredo Junior.
104
105Copyright (C) 2009 Ionzero LLC.
106
107This program is free software; you can redistribute it and/or modify it
108under the same terms as Perl itself.
109
110=cut
111
8bbd65bf 112__PACKAGE__->meta->make_immutable;
30726632 1131;