Switch subinclude generation to being called on instances.
[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/;
f97c7165 4use MooseX::Types::Moose qw/ Bool /;
8bbd65bf 5use namespace::clean -except => 'meta';
e88af283 6
7=head1 NAME
8
9Catalyst::View::Component::SubInclude::Visit - visit() plugin for C::V::Component::SubInclude
10
11=head1 VERSION
12
9399de67 13Version 0.07
e88af283 14
15=cut
16
9399de67 17our $VERSION = '0.07';
e88af283 18
19=head1 SYNOPSIS
20
21In your view class:
22
23 package MyApp::View::TT;
24 use Moose;
25
26 extends 'Catalyst::View::TT';
27 with 'Catalyst::View::Component::SubInclude';
28
29 __PACKAGE__->config( subinclude_plugin => 'Visit' );
30
31Then, somewhere in your templates:
32
33 [% subinclude('/my/widget') %]
34
35=head1 DESCRIPTION
36
37C<Catalyst::View::Component::SubInclude::Visit> uses C<< $c->visit() >> to
38render subinclude contents.
39
40This method is only supported when using L<Catalyst> version 5.71000 or newer.
41
532f3bcf 42B<WARNING: As of Catalyst version 5.71000, this plugin doesn't work for chained
43actions with captured arguments>. Apparently, C<visit> doesn't handle this type
44of actions yet.
45
e88af283 46=head1 CLASS METHODS
47
48=head2 C<generate_subinclude( $c, $path, @args )>
49
532f3bcf 50This is (roughly) equivalent to the following call:
e88af283 51
52 $c->visit( $path, @args );
53
532f3bcf 54But it will handle all the nasty details such as localizing the stash,
55parameters and response body. This is necessary to keep behavior consistent
56with the other plugins.
57
e88af283 58=cut
59
f97c7165 60has keep_stash => (
61 isa => Bool,
62 is => 'ro',
63 default => 0,
64);
65
e88af283 66sub generate_subinclude {
f97c7165 67 my ($self, $c, $path, @params) = @_;
e88af283 68
69 croak "subincludes through visit() require Catalyst version 5.71000 or newer"
70 unless $c->can('visit');
3c5cb6d6 71
72 {
f97c7165 73 local $c->{stash} = $self->keep_stash ? $c->{stash} : {};
3c5cb6d6 74
75 local $c->request->{parameters} =
76 ref $params[-1] eq 'HASH' ? pop @params : {};
77
78 local $c->response->{body};
79
9c2c47b0 80 my $captures = ref $params[0] eq 'ARRAY' ? shift @params : [];
be1a1092 81 $c->visit( $path, $captures, \@params );
3c5cb6d6 82
83 return $c->response->{body};
84 }
e88af283 85
e88af283 86}
87
88=head1 SEE ALSO
89
90L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>,
91L<Catalyst|Catalyst>
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;
e88af283 1131;