Commit | Line | Data |
30726632 |
1 | package Catalyst::View::Component::SubInclude::SubRequest; |
2 | use warnings; |
3 | use strict; |
4 | |
11a93ea1 |
5 | use Carp qw/croak/; |
6 | use namespace::clean qw/croak/; |
7 | |
4e327756 |
8 | =head1 NAME |
9 | |
10 | Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V::Component::SubInclude |
11 | |
12 | =head1 VERSION |
13 | |
e88af283 |
14 | Version 0.02 |
4e327756 |
15 | |
16 | =cut |
17 | |
e88af283 |
18 | our $VERSION = '0.02'; |
4e327756 |
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 | |
11a93ea1 |
54 | =head1 CLASS METHODS |
4e327756 |
55 | |
11a93ea1 |
56 | =head2 C<generate_subinclude( $c, $path, @args )> |
4e327756 |
57 | |
532f3bcf |
58 | This will make a sub-request call to the action specified by C<$path>. Note that |
59 | C<$path> should be the private action path - translation to the public path is |
60 | handled internally. |
4e327756 |
61 | |
532f3bcf |
62 | So, after path translation, the call will be (roughly) equivalent to: |
63 | |
64 | $c->sub_request( $translated_path, {}, @args ); |
4e327756 |
65 | |
66 | Notice that the stash will always be empty. This behavior could be configurable |
67 | in the future through an additional switch - for now, this behavior guarantees a |
532f3bcf |
68 | common interface for all plugins. |
4e327756 |
69 | |
70 | =cut |
71 | |
30726632 |
72 | sub generate_subinclude { |
4e327756 |
73 | my ($class, $c, $path, @params) = @_; |
30726632 |
74 | my $stash = {}; |
75 | |
76 | croak "subincludes through subrequests require Catalyst::Plugin::SubRequest" |
77 | unless $c->can('sub_request'); |
3c5cb6d6 |
78 | |
79 | my $args = ref $params[0] eq 'ARRAY' ? shift @params : []; |
e88af283 |
80 | |
81 | my $dispatcher = $c->dispatcher; |
3c5cb6d6 |
82 | my ($action) = $dispatcher->_invoke_as_path( $c, $path, $args ); |
30726632 |
83 | |
3c5cb6d6 |
84 | my $uri = $c->uri_for( $action, $args, @params ); |
e88af283 |
85 | |
86 | $c->sub_request( $uri->path, $stash, @params ); |
30726632 |
87 | } |
88 | |
4e327756 |
89 | =head1 SEE ALSO |
90 | |
91 | L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>, |
92 | L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest> |
93 | |
94 | =head1 AUTHOR |
95 | |
96 | Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >> |
97 | |
98 | =head1 SPONSORSHIP |
99 | |
100 | Development sponsored by Ionzero LLC L<http://www.ionzero.com/>. |
101 | |
102 | =head1 COPYRIGHT & LICENSE |
103 | |
104 | Copyright (C) 2009 Nilson Santos Figueiredo Junior. |
105 | |
106 | Copyright (C) 2009 Ionzero LLC. |
107 | |
108 | This program is free software; you can redistribute it and/or modify it |
109 | under the same terms as Perl itself. |
110 | |
111 | =cut |
112 | |
30726632 |
113 | 1; |