1 package Catalyst::View::Component::SubInclude;
5 use Catalyst::Utils ();
6 use namespace::clean -except => 'meta';
8 with 'Catalyst::Component::ContextClosure';
12 Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views
20 our $VERSION = '0.07';
24 package MyApp::View::TT;
27 extends 'Catalyst::View::TT';
28 with 'Catalyst::View::Component::SubInclude';
30 __PACKAGE__->config( subinclude_plugin => 'SubRequest' );
32 Then, somewhere in your templates:
34 [% subinclude('/my/widget') %]
35 [% subinclude_using('SubRequest', '/page/footer') %]
39 C<Catalyst::View::Component::SubInclude> allows you to include content in your
40 templates (or, more generally, somewhere in your view's C<render> processing)
41 which comes from another action in your application. It's implemented as a
42 L<Moose::Role|Moose::Role>, so using L<Moose|Moose> in your view is required.
44 Simply put, it's a way to include the output of a Catalyst sub-request somewhere
47 It's built in an extensible way so that you're free to use sub-requests, Varnish
48 ESI (L<http://www.catalystframework.org/calendar/2008/17>) or any other
49 sub-include plugin you might want to implement. An LWP plugin seems useful and
50 might be developed in the future.
52 =head1 STASH FUNCTIONS
54 This component does its magic by exporting a C<subinclude> coderef entry to the
55 stash. This way, it's easily accessible by the templates (which is the most
58 =head2 C<subinclude( $path, @args )>
60 This will render and return the body of the included resource (as specified by
61 C<$path>) using the default subinclude plugin.
63 =head2 C<subinclude_using( $plugin, $path, @args )>
65 This will render and return the body of the included resource (as specified by
66 C<$path>) using the specified subinclude plugin.
68 The C<subinclude> function above is implemented basically as a shortcut which
69 calls this function using the default plugin as the first parameter.
71 =head1 SUBINCLUDE PLUGINS
73 The module comes with two subinclude plugins:
74 L<SubRequest|Catalyst::Plugin::View::Component::SubRequest>,
75 L<Visit|Catalyst::Plugin::View::Component::Visit> and
76 L<ESI|Catalyst::Plugin::View::Component::ESI>.
78 By default, the C<SubRequest> plugin will be used. This can be changed in the
79 view's configuration options (either in the config file or in the view module
82 Configuration file example:
88 =head2 C<set_subinclude_plugin( $plugin )>
90 This method changes the current active subinclude plugin in runtime. It expects
91 the plugin suffix (e.g. C<ESI> or C<SubRequest>) or a fully-qualified class
92 name in the C<Catalyst::View::Component::SubInclude> namespace.
94 =head2 Writing plugins
96 If writing your own plugin, keep in kind plugins are required to implement a
97 class method C<generate_subinclude> with the following signature:
99 sub generate_subinclude {
100 my ($class, $c, @args) = @_;
103 The default plugin is stored in the C<subinclude_plugin> which can be changed
104 in runtime. It expects a fully qualified class name.
108 has 'subinclude_plugin' => (
113 around 'new' => sub {
117 my $self = $class->$next( @_ );
119 my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
120 $self->set_subinclude_plugin( $subinclude_plugin );
125 before 'render' => sub {
126 my ($self, $c, @args) = @_;
128 $c->stash->{subinclude} = $self->make_context_closure(sub { $self->_subinclude( @_ ) }, $c);
129 $c->stash->{subinclude_using} = $self->make_context_closure(sub { $self->_subinclude_using( @_ ) }, $c);
132 sub set_subinclude_plugin {
133 my ($self, $plugin) = @_;
135 my $subinclude_class = $self->_subinclude_plugin_class_name( $plugin );
136 $self->subinclude_plugin( $subinclude_class );
140 my ($self, $c, @args) = @_;
141 $self->_subinclude_using( $c, $self->subinclude_plugin, @args );
144 sub _subinclude_using {
145 my ($self, $c, $plugin, @args) = @_;
146 $plugin = $self->_subinclude_plugin_class_name($plugin);
147 my $plugin_config = Catalyst::Utils::merge_hashes($self->config->{subinclude}->{ALL}||{}, $self->config->{subinclude}->{$plugin}||{});
148 $plugin->generate_subinclude( $plugin_config, $c, @args );
151 sub _subinclude_plugin_class_name {
152 my ($self, $plugin) = @_;
154 # check if name is already fully qualified
155 my $pkg = __PACKAGE__;
156 return $plugin if $plugin =~ /^$pkg/;
158 my $class_name = __PACKAGE__ . '::' . $plugin;
160 eval "require $class_name";
161 croak "Error requiring $class_name: $@" if $@;
168 L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>,
169 L<Moose::Role|Moose::Role>, L<Moose|Moose>,
170 L<http://www.catalystframework.org/calendar/2008/17>
174 Please report any bugs or feature requests to
175 C<bug-catalyst-view-component-subinclude at rt.cpan.org>, or through the web interface at
176 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Component-SubInclude>.
177 I will be notified, and then you'll automatically be notified of progress on
178 your bug as I make changes.
182 Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
186 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
188 =head1 COPYRIGHT & LICENSE
190 Copyright (C) 2009 Nilson Santos Figueiredo Junior.
192 Copyright (C) 2009 Ionzero LLC.
194 This program is free software; you can redistribute it and/or modify it
195 under the same terms as Perl itself.