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