Catalyst-View-Template-Lace
view release on metacpan or search on metacpan
lib/Catalyst/View/Template/Lace/Renderer.pm view on Meta::CPAN
# Support old school Catalyst::Action::RenderView for example (
# you probably also want the ::ArgsFromStash role).
sub process {
my ($self, $c, @args) = @_;
$self->response(200, @args);
}
# helper methods
sub overlay_view {
my ($self, $view_name, $dom_proto, @args) = @_;
if( (ref($dom_proto)||'') eq 'CODE') {
local $_ = $self->dom;
@args = ($dom_proto->($self->dom), @args);
$self->dom->overlay(sub {
my $new = $self->view($view_name, @args, content=>$_)
->get_processed_dom;
return $new;
});
} elsif($dom_proto->can('each')) {
$dom_proto->each(sub {
return $self->overlay_view($view_name, $_, @args);
});
} else {
$dom_proto->overlay(sub {
return $self->view($view_name, @args, content=>$_)
->get_processed_dom;
});
}
return $self;
}
# proxy methods
sub detach { shift->ctx->detach(@_) }
lib/Catalyst/View/Template/Lace/Renderer.pm view on Meta::CPAN
$view->respond($status);
$view->respond($status, @headers);
$view->respond(@headers);
Used to setup a response. Calling this method will setup an http status, finalize
headers and set a body response for the HTML. Content type will be set to
'text/html' automatically. Status is 200 unless you specify otherwise.
=head2 overlay_view
Helper method to allow you to wrap or overlay the current view with another
view (like a master page view or some other transformation that you prefer
to have under the control of the controller). Example:
$c->view('User',
name => 'John',
age => 42,
motto => 'Why Not?')
->overlay_view(
'Master', sub {
my $user_dom = shift; # also $_ is localised to this for ease of use
title => $_->at('title')->content,
css => $_->find('link'),
meta => $_->find('meta'),
body => $_->at('body')->content}, @more_args_for_MasterView)
->http_ok;
Although you can do this via the template with components there might be cases
where you want this under the controller. For example you might use different
lib/Catalyst/View/Template/Lace/Tutorial.pod view on Meta::CPAN
This example is not intended to propose a best practice but just to show how components can interact with one another. You might decide its simpler for the child components to get their arguments directly from the containing model, for example. Or ...
There's one more component at work in this example, the 'view-master' component. This is intended to be an example of a type of master page wrapper that controls the overall basic structure of many pages on on website. Lets look again at the top of...
<view-master title=\'title:content'
css=\'@link'
meta=\'@meta'
body=\'body:content'>
So this component declares four arguments, but how are the values for these arguments populated? In this case the '\' prepended to the value indicates that the value is a reference to a node (or nodes) in the contained DOM. The idea here is that th...
package MyApp::View::Master;
use Moo;
extends 'Catalyst::View::Template::Lace';
has title => (is=>'ro', required=>1);
has css => (is=>'ro', required=>1);
has meta => (is=>'ro', required=>1);
has body => (is=>'ro', required=>1);
lib/Catalyst/View/Template/Lace/Tutorial.pod view on Meta::CPAN
</head>
<body id="body">
<h1 id="header">Intro</h1>
</body>
</html>
];
}
1;
If you are looking carefully you have noticed instead of a 'process_dom' method we have a 'on_component_add' method. We could do this with 'process_dom' but that method runs for every request and since this overlay contains no dynamic request bound ...
What might a controller that is invoking all this look like?
package MyApp::Controller::List;
use warnings;
use strict;
use base 'Catalyst::Controller';
sub display :Path('') Args(0) {
t/lib/MyApp/Controller/User.pm view on Meta::CPAN
use MooseX::MethodAttributes;
extends 'Catalyst::Controller';
sub display :Path('') {
my ($self, $c) = @_;
$c->stash(
name => 'John',
age => 42,
motto => 'Why Not?');
$c->view('User')
->overlay_view(
'Master', sub {
title => $_->at('title')->content,
css => $_->find('link'),
meta => $_->find('meta'),
body => $_->at('body')->content})
->http_ok;
}
__PACKAGE__->meta->make_immutable;
( run in 1.431 second using v1.01-cache-2.11-cpan-437f7b0c052 )