controller_role => component_role
[catagits/CatalystX-Declare.git] / examples / MyApp-Web / lib / MyApp / Web / ControllerRole / RenderView.pm
1 use CatalystX::Declare;
2
3 # almost like a normal Moose role
4 component_role MyApp::Web::ControllerRole::RenderView {
5
6     # we can use the whole Moose infrastructure
7     use MooseX::Types::Moose qw( Str );
8
9     # a normal attribute that can be passed by config
10     has default_content_type => (
11         is          => 'ro',
12         isa         => Str,
13         required    => 1,
14         default     => 'text/html; charset=utf-8',
15     );
16
17     # this private end action is a cheap ripoff of Catalyst::Action::RenderView
18     action end (@) {
19
20         # do nothing if rendering wouldn't make sense
21         return
22             if $ctx->request->method eq 'HEAD'
23             or ( defined( $ctx->response->body ) and length( $ctx->response->body ) )
24             or $ctx->response->status =~ /^(?:204|3\d\d)$/;
25
26         # set the content type from our attribute unless it is already set
27         $ctx->response->content_type( $self->default_content_type )
28             unless $ctx->response->content_type;
29
30         # find a view
31         my $view = $ctx->view
32             or die "Unable to find a view to forward to";
33
34         # and forward to it
35         $ctx->forward( $view );
36     }
37 }