Commit | Line | Data |
30726632 |
1 | package Catalyst::View::Component::SubInclude::SubRequest; |
8bbd65bf |
2 | use Moose; |
11a93ea1 |
3 | use Carp qw/croak/; |
8bbd65bf |
4 | use namespace::clean -except => 'meta'; |
11a93ea1 |
5 | |
4e327756 |
6 | =head1 NAME |
7 | |
8 | Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V::Component::SubInclude |
9 | |
10 | =head1 VERSION |
11 | |
9399de67 |
12 | Version 0.07 |
4e327756 |
13 | |
14 | =cut |
15 | |
9399de67 |
16 | our $VERSION = '0.07'; |
4e327756 |
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 | |
11a93ea1 |
52 | =head1 CLASS METHODS |
4e327756 |
53 | |
11a93ea1 |
54 | =head2 C<generate_subinclude( $c, $path, @args )> |
4e327756 |
55 | |
532f3bcf |
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. |
4e327756 |
59 | |
532f3bcf |
60 | So, after path translation, the call will be (roughly) equivalent to: |
61 | |
62 | $c->sub_request( $translated_path, {}, @args ); |
4e327756 |
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 |
532f3bcf |
66 | common interface for all plugins. |
4e327756 |
67 | |
68 | =cut |
69 | |
30726632 |
70 | sub generate_subinclude { |
7094e990 |
71 | my ($class, $config, $c, $path, @params) = @_; |
72 | my $stash = $config->{keep_stash} ? { %{ $c->stash } } : {}; |
30726632 |
73 | |
74 | croak "subincludes through subrequests require Catalyst::Plugin::SubRequest" |
75 | unless $c->can('sub_request'); |
3c5cb6d6 |
76 | |
c2d8d5b5 |
77 | my $query = ref $params[-1] eq 'HASH' ? pop @params : {}; |
e88af283 |
78 | |
c2d8d5b5 |
79 | my $action = blessed($path) |
80 | ? $path |
81 | : $c->dispatcher->get_action_by_path($path); |
30726632 |
82 | |
c2d8d5b5 |
83 | my $uri = $c->uri_for( $action, @params ); |
e88af283 |
84 | |
9c2c47b0 |
85 | $c->sub_request( $uri->path, $stash, $query ); |
30726632 |
86 | } |
87 | |
4e327756 |
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 | |
8bbd65bf |
112 | __PACKAGE__->meta->make_immutable; |
30726632 |
113 | 1; |