Bump version number and prepare for release
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude.pm
CommitLineData
eb99eb06 1package Catalyst::View::Component::SubInclude;
2use Moose::Role;
3
4use Carp qw/croak/;
5use namespace::clean qw/croak/;
3d7c8246 6use Scalar::Util qw/weaken/;
eb99eb06 7
6819a8ba 8=head1 NAME
9
10Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views
11
12=head1 VERSION
13
f71ea072 14Version 0.07
6819a8ba 15
16=cut
17
f71ea072 18our $VERSION = '0.07';
6819a8ba 19
20=head1 SYNOPSIS
21
22 package MyApp::View::TT;
23 use Moose;
24
25 extends 'Catalyst::View::TT';
26 with 'Catalyst::View::Component::SubInclude';
27
28 __PACKAGE__->config( subinclude_plugin => 'SubRequest' );
29
30Then, somewhere in your templates:
31
32 [% subinclude('/my/widget') %]
19ff3d04 33 [% subinclude_using('SubRequest', '/page/footer') %]
6819a8ba 34
35=head1 DESCRIPTION
36
37C<Catalyst::View::Component::SubInclude> allows you to include content in your
38templates (or, more generally, somewhere in your view's C<render> processing)
39which comes from another action in your application. It's implemented as a
62ebc3f4 40L<Moose::Role|Moose::Role>, so using L<Moose|Moose> in your view is required.
6819a8ba 41
42Simply put, it's a way to include the output of a Catalyst sub-request somewhere
43in your page.
44
45It's built in an extensible way so that you're free to use sub-requests, Varnish
46ESI (L<http://www.catalystframework.org/calendar/2008/17>) or any other
47sub-include plugin you might want to implement. An LWP plugin seems useful and
48might be developed in the future.
49
19ff3d04 50=head1 STASH FUNCTIONS
6819a8ba 51
52This component does its magic by exporting a C<subinclude> coderef entry to the
53stash. This way, it's easily accessible by the templates (which is the most
54common use-case).
55
56=head2 C<subinclude( $path, @args )>
57
62ebc3f4 58This will render and return the body of the included resource (as specified by
19ff3d04 59C<$path>) using the default subinclude plugin.
60
61=head2 C<subinclude_using( $plugin, $path, @args )>
62
63This will render and return the body of the included resource (as specified by
64C<$path>) using the specified subinclude plugin.
65
66The C<subinclude> function above is implemented basically as a shortcut which
67calls this function using the default plugin as the first parameter.
6819a8ba 68
69=head1 SUBINCLUDE PLUGINS
70
71The module comes with two subinclude plugins:
6cb4b001 72L<SubRequest|Catalyst::Plugin::View::Component::SubRequest>,
73L<Visit|Catalyst::Plugin::View::Component::Visit> and
6819a8ba 74L<ESI|Catalyst::Plugin::View::Component::ESI>.
75
19ff3d04 76By default, the C<SubRequest> plugin will be used. This can be changed in the
6819a8ba 77view's configuration options (either in the config file or in the view module
78itself).
79
80Configuration file example:
81
82 <View::TT>
83 subinclude_plugin ESI
84 </View::TT>
85
19ff3d04 86=head2 C<set_subinclude_plugin( $plugin )>
87
88This method changes the current active subinclude plugin in runtime. It expects
89the plugin suffix (e.g. C<ESI> or C<SubRequest>) or a fully-qualified class
90name in the C<Catalyst::View::Component::SubInclude> namespace.
91
92=head2 Writing plugins
93
8dbcfa50 94If writing your own plugin, keep in kind plugins are required to implement a
95class method C<generate_subinclude> with the following signature:
96
97 sub generate_subinclude {
98 my ($class, $c, @args) = @_;
99 }
100
19ff3d04 101The default plugin is stored in the C<subinclude_plugin> which can be changed
102in runtime. It expects a fully qualified class name.
103
6819a8ba 104=cut
105
eb99eb06 106has 'subinclude_plugin' => (
107 is => 'rw',
743822b8 108 isa => 'Str'
19ff3d04 109);
eb99eb06 110
111around 'new' => sub {
112 my $next = shift;
113 my $class = shift;
114
115 my $self = $class->$next( @_ );
116
117 my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
19ff3d04 118 $self->set_subinclude_plugin( $subinclude_plugin );
eb99eb06 119
120 $self;
121};
122
123around 'render' => sub {
124 my $next = shift;
125 my ($self, $c, @args) = @_;
3d7c8246 126
127 weaken $c;
128
19ff3d04 129 $c->stash->{subinclude} = sub { $self->_subinclude( $c, @_ ) };
130 $c->stash->{subinclude_using} = sub { $self->_subinclude_using( $c, @_ ) };
eb99eb06 131
132 $self->$next( $c, @args );
133};
134
19ff3d04 135sub set_subinclude_plugin {
136 my ($self, $plugin) = @_;
137
138 my $subinclude_class = $self->_subinclude_plugin_class_name( $plugin );
139 $self->subinclude_plugin( $subinclude_class );
140}
141
142sub _subinclude {
143 my ($self, $c, @args) = @_;
144 $self->_subinclude_using( $c, $self->subinclude_plugin, @args );
145}
146
147sub _subinclude_using {
148 my ($self, $c, $plugin, @args) = @_;
149 $plugin = $self->_subinclude_plugin_class_name($plugin);
150 $plugin->generate_subinclude( $c, @args );
151}
152
153sub _subinclude_plugin_class_name {
154 my ($self, $plugin) = @_;
155
156 # check if name is already fully qualified
157 my $pkg = __PACKAGE__;
158 return $plugin if $plugin =~ /^$pkg/;
159
160 my $class_name = __PACKAGE__ . '::' . $plugin;
161
162 eval "require $class_name";
163 croak "Error requiring $class_name: $@" if $@;
164
165 $class_name;
166}
167
6819a8ba 168=head1 SEE ALSO
169
62ebc3f4 170L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>,
171L<Moose::Role|Moose::Role>, L<Moose|Moose>,
6819a8ba 172L<http://www.catalystframework.org/calendar/2008/17>
173
174=head1 BUGS
175
176Please report any bugs or feature requests to
177C<bug-catalyst-view-component-subinclude at rt.cpan.org>, or through the web interface at
178L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Component-SubInclude>.
179I will be notified, and then you'll automatically be notified of progress on
180your bug as I make changes.
181
182=head1 AUTHOR
183
184Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
185
186=head1 SPONSORSHIP
187
188Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
189
190=head1 COPYRIGHT & LICENSE
191
192Copyright (C) 2009 Nilson Santos Figueiredo Junior.
193
194Copyright (C) 2009 Ionzero LLC.
195
196This program is free software; you can redistribute it and/or modify it
197under the same terms as Perl itself.
198
199=cut
200
eb99eb06 2011;