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