Fix everything but visit is broken
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude / SubRequest.pm
1 package Catalyst::View::Component::SubInclude::SubRequest;
2 use warnings;
3 use strict;
4
5 use Carp qw/croak/;
6 use namespace::clean qw/croak/;
7
8 =head1 NAME
9
10 Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V::Component::SubInclude
11
12 =head1 VERSION
13
14 Version 0.02
15
16 =cut
17
18 our $VERSION = '0.02';
19
20 =head1 SYNOPSIS
21
22 In your application class:
23
24   package MyApp;
25
26   use Catalyst qw/
27     ConfigLoader
28     Static::Simple
29     ...
30     SubRequest
31   /;
32
33 In 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
43 Then, somewhere in your templates:
44
45   [% subinclude('/my/widget') %]
46
47 =head1 DESCRIPTION
48
49 C<Catalyst::View::Component::SubInclude::SubRequest> uses Catalyst sub-requests
50 to render the subinclude contents. 
51
52 It requires L<Catalyst::Plugin::SubRequest>.
53
54 =head1 CLASS METHODS
55
56 =head2 C<generate_subinclude( $c, $path, @args )>
57
58 This will translate to the following sub-request call:
59
60   $c->sub_request( $path, {}, @args );
61
62 Notice that the stash will always be empty. This behavior could be configurable
63 in the future through an additional switch - for now, this behavior guarantees a
64 common interface for plugins.
65
66 =cut
67
68 sub generate_subinclude {
69     my ($class, $c, $path, @params) = @_;
70     my $stash = {};
71
72     croak "subincludes through subrequests require Catalyst::Plugin::SubRequest"
73         unless $c->can('sub_request');
74
75     my $args = ref $params[0] eq 'ARRAY' ? shift @params : [];
76     
77     my $dispatcher = $c->dispatcher;
78     my ($action) = $dispatcher->_invoke_as_path( $c, $path, $args );
79
80     my $uri = $c->uri_for( $action, $args, @params );
81
82     $c->sub_request( $uri->path, $stash, @params );
83 }
84
85 =head1 SEE ALSO
86
87 L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>, 
88 L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>
89
90 =head1 AUTHOR
91
92 Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
93
94 =head1 SPONSORSHIP
95
96 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
97
98 =head1 COPYRIGHT & LICENSE
99
100 Copyright (C) 2009 Nilson Santos Figueiredo Junior.
101
102 Copyright (C) 2009 Ionzero LLC.
103
104 This program is free software; you can redistribute it and/or modify it
105 under the same terms as Perl itself.
106
107 =cut
108
109 1;