Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views


Catalyst-View-Component-SubInclude documentation Contained in the Catalyst-View-Component-SubInclude distribution.

Index


Code Index:

NAME

Top

Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views

VERSION

Top

Version 0.10

SYNOPSIS

Top

  package MyApp::View::TT;
  use Moose;

  extends 'Catalyst::View::TT';
  with 'Catalyst::View::Component::SubInclude';

  __PACKAGE__->config( subinclude_plugin => 'SubRequest' );

Then, somewhere in your templates:

  [% subinclude('/my/widget') %]
  [% subinclude_using('SubRequest', '/page/footer') %]

DESCRIPTION

Top

Catalyst::View::Component::SubInclude allows you to include content in your templates (or, more generally, somewhere in your view's render processing) which comes from another action in your application. It's implemented as a Moose::Role, so using Moose in your view is required.

Simply put, it's a way to include the output of a Catalyst sub-request somewhere in your page.

It's built in an extensible way so that you're free to use sub-requests, Varnish ESI (http://www.catalystframework.org/calendar/2008/17) or any other sub-include plugin you might want to implement.

STASH FUNCTIONS

Top

This component does its magic by exporting a subinclude coderef entry to the stash. This way, it's easily accessible by the templates (which is the most common use-case).

subinclude( $path, @args )

This will render and return the body of the included resource (as specified by $path) using the default subinclude plugin.

subinclude_using( $plugin, $path, @args )

This will render and return the body of the included resource (as specified by $path) using the specified subinclude plugin.

The subinclude function above is implemented basically as a shortcut which calls this function using the default plugin as the first parameter.

SUBINCLUDE PLUGINS

Top

The module comes with two subinclude plugins: SubRequest (Catalyst::Plugin::View::Component::SubRequest), Visit (Catalyst::Plugin::View::Component::Visit) and ESI (Catalyst::Plugin::View::Component::ESI).

By default, the SubRequest plugin will be used. This can be changed in the view's configuration options (either in the config file or in the view module itself).

Configuration file example:

  <View::TT>
      subinclude_plugin   ESI
  </View::TT>

set_subinclude_plugin( $plugin )

This method changes the current active subinclude plugin in runtime. It expects the plugin suffix (e.g. ESI or SubRequest) or a fully-qualified class name in the Catalyst::View::Component::SubInclude namespace.

Writing plugins

If writing your own plugin, keep in kind plugins are required to implement a class method generate_subinclude with the following signature:

  sub generate_subinclude {
      my ($class, $c, @args) = @_;
  }

The default plugin is stored in the subinclude_plugin which can be changed in runtime. It expects a fully qualified class name.

SEE ALSO

Top

Catalyst::Plugin::SubRequest, Moose::Role, Moose, http://www.catalystframework.org/calendar/2008/17

BUGS

Top

Please report any bugs or feature requests to bug-catalyst-view-component-subinclude at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Component-SubInclude. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

AUTHOR

Top

Nilson Santos Figueiredo Junior, <nilsonsfj at cpan.org>

CONTRIBUTORS

Top

Tomas Doran (t0m) <bobtfish@bobtfish.net.

Vladimir Timofeev, <vovkasm at gmail.com>.

Wallace Reis (wreis) <wreis@cpan.org>.

SPONSORSHIP

Top

Development sponsored by Ionzero LLC http://www.ionzero.com/.

COPYRIGHT & LICENSE

Top


Catalyst-View-Component-SubInclude documentation Contained in the Catalyst-View-Component-SubInclude distribution.
package Catalyst::View::Component::SubInclude;
use Moose::Role;

use Carp qw/croak/;
use Catalyst::Utils ();
use Class::MOP ();
use MooseX::Types::Moose qw/Str HashRef/;
use namespace::clean -except => 'meta';

with 'Catalyst::Component::ContextClosure';

our $VERSION = '0.10';
$VERSION = eval $VERSION;

has 'subinclude_plugin' => (
    is => 'rw',
    isa => Str,
);

has subinclude => (
    is => 'ro',
    isa => HashRef,
    default => sub { {} },
);

around 'new' => sub {
    my $next = shift;
    my $class = shift;

    my $self = $class->$next( @_ );

    my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
    $self->set_subinclude_plugin( $subinclude_plugin );

    $self;
};

before 'render' => sub {
    my ($self, $c, @args) = @_;

    $c->stash->{subinclude}       = $self->make_context_closure(sub { $self->_subinclude( @_ ) }, $c);
    $c->stash->{subinclude_using} = $self->make_context_closure(sub { $self->_subinclude_using( @_ ) }, $c);
};

sub set_subinclude_plugin {
    my ($self, $plugin) = @_;

    my $subinclude_class = blessed $self->_subinclude_plugin_class_instance( $plugin );
    $self->subinclude_plugin( $subinclude_class );
}

sub _subinclude {
    my ($self, $c, @args) = @_;
    $self->_subinclude_using( $c, $self->subinclude_plugin, @args );
}

sub _subinclude_using {
    my ($self, $c, $plugin, @args) = @_;
    $plugin = $self->_subinclude_plugin_class_instance($plugin);
    $plugin->generate_subinclude( $c, @args );
}

has _subinclude_plugin_class_instance_cache => (
    isa => HashRef,
    is => 'ro',
    default => sub { {} },
);

sub _subinclude_plugin_class_instance {
    my ($self, $plugin) = @_;

    my $cache = $self->_subinclude_plugin_class_instance_cache;
    return $cache->{$plugin} if exists $cache->{$plugin};

    my $plugin_config = Catalyst::Utils::merge_hashes(
        $self->subinclude->{ALL}||{},
        $self->subinclude->{$plugin}||{}
    );
    my $short_class = $plugin_config->{'class'} ?
        delete $plugin_config->{'class'}
        : $plugin;
    my $class = $short_class =~ /::/ ?
        $short_class
        : __PACKAGE__ . '::' . $short_class;

    Class::MOP::load_class($class);

    return $cache->{$class} = $class->new($plugin_config);
}

1;