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