Add MANIFEST and META.yml
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude.pm
CommitLineData
30726632 1package Catalyst::View::Component::SubInclude;
2use Moose::Role;
3
4use Carp qw/croak/;
5use namespace::clean qw/croak/;
6
ada1ae3c 7=head1 NAME
8
9Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views
10
11=head1 VERSION
12
e88af283 13Version 0.02
ada1ae3c 14
15=cut
16
e88af283 17our $VERSION = '0.02';
ada1ae3c 18
19=head1 SYNOPSIS
20
21 package MyApp::View::TT;
22 use Moose;
23
24 extends 'Catalyst::View::TT';
25 with 'Catalyst::View::Component::SubInclude';
26
27 __PACKAGE__->config( subinclude_plugin => 'SubRequest' );
28
29Then, somewhere in your templates:
30
31 [% subinclude('/my/widget') %]
32
33=head1 DESCRIPTION
34
35C<Catalyst::View::Component::SubInclude> allows you to include content in your
36templates (or, more generally, somewhere in your view's C<render> processing)
37which comes from another action in your application. It's implemented as a
4e327756 38L<Moose::Role|Moose::Role>, so using L<Moose|Moose> in your view is required.
ada1ae3c 39
40Simply put, it's a way to include the output of a Catalyst sub-request somewhere
41in your page.
42
43It's built in an extensible way so that you're free to use sub-requests, Varnish
44ESI (L<http://www.catalystframework.org/calendar/2008/17>) or any other
45sub-include plugin you might want to implement. An LWP plugin seems useful and
46might be developed in the future.
47
48=head1 STASH FUNCTION
49
50This component does its magic by exporting a C<subinclude> coderef entry to the
51stash. This way, it's easily accessible by the templates (which is the most
52common use-case).
53
54=head2 C<subinclude( $path, @args )>
55
4e327756 56This will render and return the body of the included resource (as specified by
57C<$path>).
ada1ae3c 58
59=head1 SUBINCLUDE PLUGINS
60
61The module comes with two subinclude plugins:
e88af283 62L<SubRequest|Catalyst::Plugin::View::Component::SubRequest>,
63L<Visit|Catalyst::Plugin::View::Component::Visit> and
ada1ae3c 64L<ESI|Catalyst::Plugin::View::Component::ESI>.
65
66By default, the SubRequest plugin will be used. This can be changed in the
67view's configuration options (either in the config file or in the view module
68itself).
69
70Configuration file example:
71
72 <View::TT>
73 subinclude_plugin ESI
74 </View::TT>
75
11a93ea1 76If writing your own plugin, keep in kind plugins are required to implement a
77class method C<generate_subinclude> with the following signature:
78
79 sub generate_subinclude {
80 my ($class, $c, @args) = @_;
81 }
82
ada1ae3c 83=cut
84
30726632 85has 'subinclude_plugin' => (
86 is => 'rw',
87 isa => 'ClassName'
88);
89
90around 'new' => sub {
91 my $next = shift;
92 my $class = shift;
93
94 my $self = $class->$next( @_ );
95
96 my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
97 my $subinclude_class = __PACKAGE__ . '::' . $subinclude_plugin;
98
99 eval "require $subinclude_class";
100 croak "Error requiring $subinclude_class: $@" if $@;
101
102 $self->subinclude_plugin( $subinclude_class );
103
104 $self;
105};
106
107around 'render' => sub {
108 my $next = shift;
109 my ($self, $c, @args) = @_;
110
111 $c->stash->{subinclude} = sub {
112 $self->subinclude_plugin->generate_subinclude( $c, @_ );
113 };
114
115 $self->$next( $c, @args );
116};
117
ada1ae3c 118=head1 SEE ALSO
119
4e327756 120L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>,
121L<Moose::Role|Moose::Role>, L<Moose|Moose>,
ada1ae3c 122L<http://www.catalystframework.org/calendar/2008/17>
123
124=head1 BUGS
125
126Please report any bugs or feature requests to
127C<bug-catalyst-view-component-subinclude at rt.cpan.org>, or through the web interface at
128L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Component-SubInclude>.
129I will be notified, and then you'll automatically be notified of progress on
130your bug as I make changes.
131
132=head1 AUTHOR
133
134Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
135
136=head1 SPONSORSHIP
137
138Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
139
140=head1 COPYRIGHT & LICENSE
141
142Copyright (C) 2009 Nilson Santos Figueiredo Junior.
143
144Copyright (C) 2009 Ionzero LLC.
145
146This program is free software; you can redistribute it and/or modify it
147under the same terms as Perl itself.
148
149=cut
150
30726632 1511;