File Coverage

blib/lib/Catalyst/View/Download/HTML.pm
Criterion Covered Total %
statement 11 17 64.7
branch 0 2 0.0
condition 1 3 33.3
subroutine 3 4 75.0
pod 2 2 100.0
total 17 28 60.7


line stmt bran cond sub pod time code
1             package Catalyst::View::Download::HTML;
2              
3 2     2   1962 use Moose;
  2         2  
  2         12  
4 2     2   8197 use namespace::autoclean;
  2         878  
  2         12  
5              
6             extends 'Catalyst::View';
7              
8             =head1 NAME
9              
10             Catalyst::View::Download::HTML
11              
12             =head1 VERSION
13              
14             0.05
15              
16             =cut
17              
18             our $VERSION = "0.05";
19             $VERSION = eval $VERSION;
20              
21             __PACKAGE__->config( 'stash_key' => 'html' );
22              
23             sub process {
24 0     0 1 0 my $self = shift;
25 0         0 my ( $c ) = @_;
26              
27 0         0 my $template = $c->stash->{ 'template' };
28 0         0 my $content = $self->render( $c, $template, $c->stash );
29              
30 0 0       0 $c->res->headers->header( "Content-Type" => "text/html" )
31             if ( $c->res->headers->header( "Content-Type" ) eq "" );
32 0         0 $c->res->body( $content );
33             }
34              
35             sub render {
36 1     1 1 707 my $self = shift;
37 1         3 my ( $c, $template, $args ) = @_;
38              
39 1         3 my $stash_key = $self->config->{ 'stash_key' };
40 1   33     49 my $content = $c->stash->{ $stash_key } || $c->response->body;
41              
42 1         74 return $content;
43             }
44              
45             __PACKAGE__->meta->make_immutable;
46              
47             1;
48              
49             __END__
50              
51             =head1 SYNOPSIS
52              
53             # lib/MyApp/View/Download/HTML.pm
54             package MyApp::View::Download::HTML;
55             use base qw( Catalyst::View::Download::HTML );
56             1;
57              
58             # lib/MyApp/Controller/SomeController.pm
59             sub example_action_1 : Local {
60             my ($self, $c) = @_;
61              
62             my $content = "Some Text";
63              
64             # To output your data just pass your content into the 'html' key of the stash
65             $c->stash->{'html'} = $content;
66              
67             # Or into the body of the response for this action
68             $c->response->body($content);
69              
70             # Finally forward processing to the HTML View
71             $c->forward('MyApp::View::Download::HTML');
72             }
73              
74             =head1 DESCRIPTION
75              
76             Takes content and outputs the content as html text.
77              
78             =head1 SUBROUTINES
79              
80             =head2 process
81              
82             This method will be called by Catalyst if it is asked to forward to a component
83             without a specified action.
84              
85             =head2 render
86              
87             Allows others to use this view for much more fine-grained content generation.
88              
89             =head1 CONFIG
90              
91             =over 4
92              
93             =item stash_key
94              
95             Determines the key in the stash this view will look for when attempting to
96             retrieve content to process. If this key isn't found it will then look at
97             $c->response->body for content.
98              
99             $c->view('MyApp::View::Download::HTML')->config->{'stash_key'} = 'content';
100              
101             =back
102              
103             =head1 AUTHOR
104              
105             Travis Chase, C<< <gaudeon at cpan dot org> >>
106              
107             =head1 SEE ALSO
108              
109             L<Catalyst> L<Catalyst::View> L<Catalyst::View::Download>
110              
111             =head1 LICENSE
112              
113             Copyright 2011 Travis Chase.
114              
115             This program is free software. You can redistribute it and/or modify it
116             under the same terms as Perl itself.
117              
118             =cut