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