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