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