line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Kelp::Module::Template; |
2
|
|
|
|
|
|
|
|
3
|
29
|
|
|
29
|
|
13749
|
use Kelp::Base 'Kelp::Module'; |
|
29
|
|
|
|
|
68
|
|
|
29
|
|
|
|
|
173
|
|
4
|
29
|
|
|
29
|
|
15549
|
use Kelp::Template; |
|
29
|
|
|
|
|
93
|
|
|
29
|
|
|
|
|
155
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
attr ext => 'tt'; |
7
|
|
|
|
|
|
|
attr engine => sub { die "'engine' must be initialized" }; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub build { |
10
|
38
|
|
|
38
|
1
|
125
|
my ( $self, %args ) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Build and initialize the engine attribute |
13
|
38
|
|
|
|
|
155
|
$self->engine( $self->build_engine(%args) ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Register one method - template |
16
|
|
|
|
|
|
|
$self->register( |
17
|
|
|
|
|
|
|
template => sub { |
18
|
41
|
|
|
41
|
|
1465
|
my ( $app, $template, $vars, @rest ) = @_; |
19
|
41
|
|
|
|
|
165
|
return $self->render( $self->_rename($template), $vars, @rest ); |
20
|
|
|
|
|
|
|
} |
21
|
38
|
|
|
|
|
377
|
); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub build_engine { |
25
|
37
|
|
|
37
|
1
|
104
|
my ( $self, %args ) = @_; |
26
|
37
|
|
|
|
|
211
|
return Kelp::Template->new( %args ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub render { |
30
|
39
|
|
|
39
|
1
|
105
|
my ( $self, $template, $vars ) = @_; |
31
|
39
|
|
|
|
|
125
|
return $self->engine->process( $template, $vars ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _rename { |
35
|
44
|
|
|
44
|
|
113
|
my ( $self, $name ) = @_; |
36
|
44
|
100
|
|
|
|
119
|
return unless $name; |
37
|
|
|
|
|
|
|
|
38
|
43
|
100
|
100
|
|
|
279
|
return $name if ref($name) || $name =~ /\.(.+)$/; |
39
|
|
|
|
|
|
|
|
40
|
37
|
|
|
|
|
170
|
my $ext = $self->ext; |
41
|
37
|
100
|
66
|
|
|
306
|
return (defined $ext && length $ext) ? "$name.$ext" : $name; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
1; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
__END__ |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=pod |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Kelp::Module::Template - Template processing for Kelp applications |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 SYNOPSIS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
First ... |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# conf/config.pl |
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
modules => ['Template'], |
61
|
|
|
|
|
|
|
modules_init => { |
62
|
|
|
|
|
|
|
Template => { ... } |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Then ... |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# lib/MyApp.pm |
69
|
|
|
|
|
|
|
sub some_route { |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
$self->template('some_template', { bar => 'foo' }); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 DESCRIPTION |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This module provides an interface for using templates in a Kelp web application. It |
77
|
|
|
|
|
|
|
uses L<Kelp::Template>, but it could be easily subclassed to use anything else. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 REGISTERED METHODS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 template |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
C<template($filename, \%vars)> |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Renders a file using the currently loaded template engine. If the file doesn't |
86
|
|
|
|
|
|
|
have an extension, the one specified in L</ext> will be assigned to it. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 ext |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
The default extension of the template files. This module sets this attribute to |
93
|
|
|
|
|
|
|
C<tt>, so |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
$self->template( 'home' ); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
will look for C<home.tt>. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 engine |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
This attribute will be initialized by the C<build_engine> method of this module, |
102
|
|
|
|
|
|
|
and it is available to all code that needs access to the template engine |
103
|
|
|
|
|
|
|
instance. See L</SUBCLASSING> for an example. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 METHODS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 build_engine |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
C<build_engine(%args)> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This method is responsible for creating, initializing and returning an instance |
112
|
|
|
|
|
|
|
of the template engine used, for example L<Template>. Override it to use a |
113
|
|
|
|
|
|
|
different template engine, for example L<Text::Haml>. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 render |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
C<render($template, \%vars, @rest)> |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
This method should return a rendered text. Override it if you're subclassing and |
120
|
|
|
|
|
|
|
using a different template engine. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 PERKS |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 UTF8 |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
To process templates in utf8, add the C<encoding> to the module configuration: |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# conf/config.pl |
129
|
|
|
|
|
|
|
{ |
130
|
|
|
|
|
|
|
modules => ['Template'], |
131
|
|
|
|
|
|
|
modules_init => { |
132
|
|
|
|
|
|
|
Template => { |
133
|
|
|
|
|
|
|
encoding => 'utf8' |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
}; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 SUBCLASSING |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
To use a different template engine, you can subclass this module. You will need |
141
|
|
|
|
|
|
|
to make sure your new class does the following (for the sake of the example we |
142
|
|
|
|
|
|
|
will show you how to create a L<Text::Haml> rendering module): |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Overrides the L</ext> attribute and provides the file extension of the new |
149
|
|
|
|
|
|
|
template files. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
attr ext => 'haml'; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Overrides the L</build_engine> method and creates an instance of the new |
156
|
|
|
|
|
|
|
template engine. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub build_engine { |
159
|
|
|
|
|
|
|
my ( $self, %args ) = @_; |
160
|
|
|
|
|
|
|
return Text::Haml->new( %args ); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Overrides the L</render> method and renders using C<$self-E<gt>engine>. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub render { |
168
|
|
|
|
|
|
|
my ( $self, $template, $vars, @rest ) = @_; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Get the template engine instance |
171
|
|
|
|
|
|
|
my $haml = $self->engine; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# If the $template is a reference, then render string, |
174
|
|
|
|
|
|
|
# otherwise it's a file name. |
175
|
|
|
|
|
|
|
return ref($template) eq 'SCALAR' |
176
|
|
|
|
|
|
|
? $haml->render( $$template, %$vars ) |
177
|
|
|
|
|
|
|
: $haml->render_file( $template, %$vars ); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |