Make the subrequest plugins Moose classes with a constructor
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude / Visit.pm
CommitLineData
e88af283 1package Catalyst::View::Component::SubInclude::Visit;
8bbd65bf 2use Moose;
e88af283 3use Carp qw/croak/;
8bbd65bf 4use namespace::clean -except => 'meta';
e88af283 5
6=head1 NAME
7
8Catalyst::View::Component::SubInclude::Visit - visit() plugin for C::V::Component::SubInclude
9
10=head1 VERSION
11
9399de67 12Version 0.07
e88af283 13
14=cut
15
9399de67 16our $VERSION = '0.07';
e88af283 17
18=head1 SYNOPSIS
19
20In your view class:
21
22 package MyApp::View::TT;
23 use Moose;
24
25 extends 'Catalyst::View::TT';
26 with 'Catalyst::View::Component::SubInclude';
27
28 __PACKAGE__->config( subinclude_plugin => 'Visit' );
29
30Then, somewhere in your templates:
31
32 [% subinclude('/my/widget') %]
33
34=head1 DESCRIPTION
35
36C<Catalyst::View::Component::SubInclude::Visit> uses C<< $c->visit() >> to
37render subinclude contents.
38
39This method is only supported when using L<Catalyst> version 5.71000 or newer.
40
532f3bcf 41B<WARNING: As of Catalyst version 5.71000, this plugin doesn't work for chained
42actions with captured arguments>. Apparently, C<visit> doesn't handle this type
43of actions yet.
44
e88af283 45=head1 CLASS METHODS
46
47=head2 C<generate_subinclude( $c, $path, @args )>
48
532f3bcf 49This is (roughly) equivalent to the following call:
e88af283 50
51 $c->visit( $path, @args );
52
532f3bcf 53But it will handle all the nasty details such as localizing the stash,
54parameters and response body. This is necessary to keep behavior consistent
55with the other plugins.
56
e88af283 57=cut
58
59sub generate_subinclude {
7094e990 60 my ($class, $config, $c, $path, @params) = @_;
e88af283 61
62 croak "subincludes through visit() require Catalyst version 5.71000 or newer"
63 unless $c->can('visit');
3c5cb6d6 64
65 {
7094e990 66 local $c->{stash} = $config->{keep_stash} ? $c->{stash} : {};
3c5cb6d6 67
68 local $c->request->{parameters} =
69 ref $params[-1] eq 'HASH' ? pop @params : {};
70
71 local $c->response->{body};
72
9c2c47b0 73 my $captures = ref $params[0] eq 'ARRAY' ? shift @params : [];
be1a1092 74 $c->visit( $path, $captures, \@params );
3c5cb6d6 75
76 return $c->response->{body};
77 }
e88af283 78
e88af283 79}
80
81=head1 SEE ALSO
82
83L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>,
84L<Catalyst|Catalyst>
85
86=head1 AUTHOR
87
88Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
89
90=head1 SPONSORSHIP
91
92Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
93
94=head1 COPYRIGHT & LICENSE
95
96Copyright (C) 2009 Nilson Santos Figueiredo Junior.
97
98Copyright (C) 2009 Ionzero LLC.
99
100This program is free software; you can redistribute it and/or modify it
101under the same terms as Perl itself.
102
103=cut
104
8bbd65bf 105__PACKAGE__->meta->make_immutable;
e88af283 1061;