File Coverage

blib/lib/Kelp/Module/Template.pm
Criterion Covered Total %
statement 20 20 100.0
branch 6 6 100.0
condition 5 6 83.3
subroutine 7 7 100.0
pod 3 3 100.0
total 41 42 97.6


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