e256a0f3a26eae9f5ac306cdc0300bfe72a9a0ed
[catagits/Catalyst-View-ContentNegotiation-XHTML.git] / lib / Catalyst / View / TT / XHTML.pm
1 package Catalyst::View::TT::XHTML;
2 use strict;
3 use warnings;
4 use HTTP::Negotiate qw(choose);
5 use base qw/Catalyst::View::TT/;
6
7 our $VERSION = '1.001';
8
9 our $variants = [
10     [qw| xhtml 1.000 application/xhtml+xml |],
11     [qw| html  0.001 text/html             |],
12 ];
13
14 sub process {
15     my $self = shift;
16     my ($c) = @_;
17     my $return = $self->next::method(@_);
18    
19     if ($c->request->header('Accept') && $c->response->headers->{'content-type'} =~ m|text/html|) {
20         my $var = choose($variants, $c->request->headers);
21         if ($var eq 'xhtml') {
22             $c->response->headers->{'content-type'} =~ s|text/html|application/xhtml+xml|;
23         }
24     }
25     return $return;
26 }
27
28 1;
29
30 __END__
31
32 =head1 NAME
33
34 Catalyst::View::TT::XHTML - A sub-class of the standard TT view which
35 serves application/xhtml+xml content if the browser accepts it.
36
37 =head1 SYNOPSIS
38
39     package MyApp::View::XHTML;
40     use strict;
41     use warnings;
42     use base qw/Catalyst::View::TT::XHTML MyApp::View::TT/;
43     
44     1;
45     
46 =head1 DESCRIPTION
47
48 This is a very simple sub-class of L<Catalyst::View::TT>, which sets
49 the response C<Content-Type> to be C<application/xhtml+xml> if the
50 user's browser sends an C<Accept> header indicating that it is willing
51 to process that MIME type.
52
53 Changing the C<Content-Type> causes browsers to interpret the page as
54 strict XHTML, meaning that the markup must be well formed.
55
56 This is useful when you're developing your application, as you know that
57 all pages you view are rendered strictly, so any markup errors will show
58 up at once.
59
60 =head1 METHODS
61
62 =head2 process
63
64 Overrides the standard process method, delegating to L<Catalyst::View::TT>
65 to render the template, and then changing the response C<Content-Type> if
66 appropriate (from the requests C<Accept> header).
67
68 =head1 BUGS
69
70 There should be a more elegant way to inherit the config of your normal 
71 TT view.
72
73 Configuration (as loaded by L<Catalyst::Plugin::ConfigLoader>) for the TT 
74 view is not used.
75
76 No helper to generate the view file needed (just copy the code in the 
77 SYNOPSIS).
78
79 =head1 AUTHOR
80
81 Tomas Doran C<< <bobtfish@bobtfish.net> >>
82
83 =head1 CONTRIBUTORS
84
85 =over
86
87 =item David Dorward - test patches
88
89 =back
90
91 =head1 COPYRIGHT
92
93 This module itself is copyright (c) 2008 Tomas Doran and is licensed under the same terms as Perl itself.
94
95 =cut