[Catalyst] Building complex layouts

Octavian Râşniţă orasnita at gmail.com
Wed Dec 30 11:47:53 GMT 2009


From: "Pavel O. Karoukin" <pavel at yepcorp.com>

> Hello,
>
> I wonder what is a best practice to build complex layouts (sidebars, forms
> in 'em, list of latest news in sidebar, etc)?
> Right now I see it in chaining several actions and each one adding content
> into stash. Is there any other dry approaches?
> May be there is a way to separate each element of layout into it's own
> action + template and call this from "base" template?

You can do:

sub main_action : Path {
  my ($self, $c) = @_;
  $c->forward('action1');
  $c->forward('action2', ['arg1', 'arg2']);
  #...
}

sub action1 : Private {
  my ($self, $c) = @_;
  #Get the content, eventually get/put it in/from the cache, and stuff it in 
the stash
  $c->stash(first_component => $content);
}

sub action2 : Private {
  my ($self, $c, $arg1, $arg2) = @_;
  # Same as previous
}

The template of the main_action will be able to use 
$c->stash->{first_component} or other variables you will put in the stash.
The results of the private subroutines can be used in more public actions, 
can be cached individually and their content can be created using their own 
templates, with a code like:

my $body_of_the_action = $c->view('TT')->render($c, 'path/to/template.tt');

Octavian




More information about the Catalyst mailing list