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

Revision 2112 (checked in by miyagawa, 14 years ago)

added Helper: releng

  • Property svn:keywords set to Id Revision
Line 
1 package Catalyst::View::Jemplate;
2
3 use strict;
4 our $VERSION = '0.03';
5
6 use base qw( Catalyst::View );
7 use File::Find::Rule;
8 use Jemplate;
9 use NEXT;
10 use Path::Class;
11
12 __PACKAGE__->mk_accessors(qw( jemplate_dir jemplate_ext encoding ));
13
14 sub new {
15     my($class, $c, $arguments) = @_;
16     my $self = $class->NEXT::new($c);
17
18     $self->jemplate_dir($arguments->{jemplate_dir});
19     $self->jemplate_ext($arguments->{jemplate_ext} || '.tt');
20     $self->encoding($arguments->{encoding} || 'utf-8');
21
22     my $dir = $self->jemplate_dir
23         or Catalyst::Exception->throw("jemplate_dir needed");
24
25     unless (-e $dir && -d _) {
26         Catalyst::Exception->throw("$dir: $!");
27     }
28
29     $self;
30 }
31
32 sub process {
33     my($self, $c) = @_;
34
35     my $data   = $c->stash->{jemplate};
36     my $cache  = $c->can('cache') ? $c->cache("jemplate") : undef;
37     my $output = '';
38
39     my $cache_key = $data->{key} || $c->req->match;
40     if ($cache) {
41         $output = $cache->get($cache_key);
42         if ($c->log->is_debug) {
43             if ($output) {
44                 $c->log->debug("Catalyst::View::Jemplate cache HIT for $cache_key");
45             } else {
46                 $c->log->debug("Catalyst::View::Jemplate cache MISS for $cache_key");
47             }
48         }
49     }
50
51     if (! $output) {
52         # We aren't cached, or we don't have a cache configured for us
53         my @files;
54
55         if ($data && $data->{files}) {
56             # The user can specify exactly which files we include in this
57             # particular dispatch
58             @files =
59                 map { file($self->jemplate_dir, $_) }
60                 ref($data->{files}) ? @{ $data->{files} } : ($data->{files})
61             ;
62         } else {
63             # XXX - not a good idea, but leave it as final alternative
64             @files = File::Find::Rule->file
65                                      ->name( '*' . $self->jemplate_ext )
66                                      ->in( $self->jemplate_dir );
67         }
68
69         if ($c->log->is_debug) {
70             $c->log->debug("Creating Jemplate file from @files");
71         }
72
73         # xxx error handling?
74         $output = Jemplate->compile_template_files(@files);
75         if ($cache) {
76             $cache->set($cache_key, $output);
77         }
78     }
79
80     my $encoding = $self->encoding || 'utf-8';
81     if (($c->req->user_agent || '') =~ /Opera/) {
82         $c->res->content_type("application/x-javascript; charset=$encoding");
83     } else {
84         $c->res->content_type("text/javascript; charset=$encoding");
85     }
86
87     $c->res->output($output || '');
88 }
89
90 1;
91 __END__
92
93 =head1 NAME
94
95 Catalyst::View::Jemplate - Jemplate files server
96
97 =head1 SYNOPSIS
98
99   package MyApp::View::Jemplate;
100   use base qw( Catalyst::View::Jemplate );
101
102   package MyApp;
103
104   MyApp->config(
105       'View::Jemplate' => {
106           jemplate_dir => MyApp->path_to('root', 'jemplate'),
107           jemplate_ext => '.tt',
108       },
109   );
110
111   sub jemplate : Global {
112       my($self, $c) = @_;
113       $c->forward('View::Jemplate');
114   }
115
116   # To specify which files you want to include
117   sub select : Global {
118       my($self, $c) = @_;
119       $c->stash->{jemplate} = {
120           files => [ 'foo.tt', 'bar.tt' ]
121       }
122   }
123
124   # To use caching
125   use Catalyst qw(
126       ...
127       Cache
128   );
129
130   MyApp->config(
131       cache => {
132           backends => {
133               jemplate => {
134                   # Your cache backend of choice
135                   store => "FastMmap",
136               }
137           }
138       }
139   );
140      
141
142 =head1 DESCRIPTION
143
144 Catalyst::View::Jemplate is a Catalyst View plugin to automatically
145 compile TT files into JavaScript, using ingy's Jemplate.
146
147 Instead of creating the compiled javascript files by-hand, you can
148 include the file via Catalyst app like:
149
150   <script src="js/Jemplate.js" type="text/javascript"></script>
151   <script src="/jemplate/all.js" type="text/javascript"></script>
152
153 =head1 TODO
154
155 =over 4
156
157 =item *
158
159 Yeah, we definitely need a cache. For now it compiles templates in
160 every request, which is not very efficient.
161
162 =item *
163
164 Right now all the template files under C<jemplate_dir> is compiled
165 into a single JavaScript file and served. Probably we need a path
166 option to limit the directory.
167
168 =cut
169
170 =head1 AUTHOR
171
172 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
173
174 This library is free software; you can redistribute it and/or modify
175 it under the same terms as Perl itself.
176
177 =head1 SEE ALSO
178
179 L<>
180
181 =cut
Note: See TracBrowser for help on using the browser.