root/Catalyst-View-Jemplate/trunk/lib/Catalyst/View/Jemplate.pm

Revision 1778 (checked in by miyagawa, 15 years ago)

import Catalyst-View-Jemplate

  • Property svn:keywords set to Id Revision
Line 
1 package Catalyst::View::Jemplate;
2
3 use strict;
4 our $VERSION = '0.01';
5
6 use base qw( Catalyst::View );
7 use File::Find::Rule;
8 use Jemplate;
9 use NEXT;
10
11 __PACKAGE__->mk_accessors(qw( jemplate_dir jemplate_ext encoding ));
12
13 sub new {
14     my($class, $c, $arguments) = @_;
15     my $self = $class->NEXT::new($c);
16
17     $self->jemplate_dir($arguments->{jemplate_dir});
18     $self->jemplate_ext($arguments->{jemplate_ext} || '.tt');
19     $self->encoding($arguments->{encoding} || 'utf-8');
20
21     my $dir = $self->jemplate_dir
22         or Catalyst::Exception->throw("jemplate_dir needed");
23
24     unless (-e $dir && -d _) {
25         Catalyst::Exception->throw("$dir: $!");
26     }
27
28     $self;
29 }
30
31 sub process {
32     my($self, $c) = @_;
33
34     my @files = File::Find::Rule->file
35                                 ->name( '*' . $self->jemplate_ext )
36                                 ->in( $self->jemplate_dir );
37
38     # xxx error handling?
39     my $js = Jemplate->compile_templates(@files);
40
41     my $encoding = $self->encoding || 'utf-8';
42     if (($c->req->user_agent || '') =~ /Opera/) {
43         $c->res->content_type("application/x-javascript; charset=$encoding");
44     } else {
45         $c->res->content_type("text/javascript; charset=$encoding");
46     }
47
48     $c->res->output($js);
49 }
50
51 1;
52 __END__
53
54 =head1 NAME
55
56 Catalyst::View::Jemplate - Jemplate files server
57
58 =head1 SYNOPSIS
59
60   package MyApp::View::Jemplate;
61   use base qw( Catalyst::View::Jemplate );
62
63   package MyApp;
64
65   MyApp->config(
66       'View::Jemplate' => {
67           jemplate_dir => MyApp->path_to('root', 'jemplate'),
68           jemplate_ext => '.tt',
69       },
70   );
71
72   sub jemplate : Global {
73       my($self, $c) = @_;
74       $c->forward('View::Jemplate');
75   }
76
77 =head1 DESCRIPTION
78
79 Catalyst::View::Jemplate is a Catalyst View plugin to automatically
80 compile TT files into JavaScript, using ingy's Jemplate.
81
82 Instead of creating the compiled javascript files by-hand, you can
83 include the file via Catalyst app like:
84
85   <script src="js/Jemplate.js" type="text/javascript"></script>
86   <script src="/jemplate/all.js" type="text/javascript"></script>
87
88 =head1 TODO
89
90 =over 4
91
92 =item *
93
94 Yeah, we definitely need a cache. For now it compiles templates in
95 every request, which is not very efficient.
96
97 =item *
98
99 Right now all the template files under C<jemplate_dir> is compiled
100 into a single JavaScript file and served. Probably we need a path
101 option to limit the directory.
102
103 =cut
104
105 =head1 AUTHOR
106
107 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
108
109 This library is free software; you can redistribute it and/or modify
110 it under the same terms as Perl itself.
111
112 =head1 SEE ALSO
113
114 L<>
115
116 =cut
Note: See TracBrowser for help on using the browser.