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