File Coverage

blib/lib/Dancer/Template/Tenjin.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Dancer::Template::Tenjin;
2              
3             # ABSTRACT: Tenjin wrapper for Dancer
4              
5 1     1   24554 use strict;
  1         3  
  1         37  
6 1     1   5 use warnings;
  1         2  
  1         51  
7              
8             our $VERSION = "0.5";
9             $VERSION = eval $VERSION;
10              
11 1     1   857 use Tenjin 0.070001;
  1         22679  
  1         43  
12 1     1   492 use Dancer::Config 'setting';
  0            
  0            
13             use File::Basename;
14             use Try::Tiny;
15             use Carp;
16              
17             use base 'Dancer::Template::Abstract';
18              
19             =head1 NAME
20              
21             Dancer::Template::Tenjin - Tenjin wrapper for Dancer
22              
23             =head1 VERSION
24              
25             version 0.5
26              
27             =head1 SYNOPSIS
28              
29             # in your config.yml
30             template: "tenjin"
31              
32             # note: templates must use the '.tt' extension
33              
34             # you might also want to add (if your templates are UTF-8, which is the
35             # default encoding used by Tenjin):
36             charset: "UTF-8"
37              
38             =head1 DESCRIPTION
39              
40             This class is an interface between Dancer's template engine abstraction layer
41             and the L module.
42              
43             Tenjin is a fast and feature-full templating engine that can be used by
44             Dancer for production purposes. In comparison to L,
45             it is much more lightweight, has almost no dependencies, and supports
46             embedded Perl code instead of defining its own language.
47              
48             In order to use this engine, you need to set your webapp's template engine
49             in your app's configuration file (config.yml) like so:
50              
51             template: "tenjin"
52              
53             You can also directly set it in your app code with the B keyword.
54              
55             Now you can create Tenjin templates normally, but note that due to a
56             Dancer restriction your template files must end in the '.tt' extension as
57             Dancer automatically adds this extension to the template names you declare
58             in your apps.
59              
60             =head1 METHODS
61              
62             =head2 init()
63              
64             Initializes a template engine by generating a new instance of L.
65              
66             =cut
67              
68             sub init {
69             $_[0]->{engine} = Tenjin->new({ postfix => '.tt', path => [setting('views')] });
70             }
71              
72             =head2 render( $template, $tokens )
73              
74             Receives a template name and a hash-ref of key-value pairs to pass to
75             the template, and returns the template rendered by Tenjin.
76              
77             =cut
78              
79             sub render($$$) {
80             my ($self, $template, $tokens) = @_;
81              
82             croak "'$template' is not a regular file"
83             if !ref $template && !-f $template;
84              
85             $tokens ||= {};
86              
87             # Dancer seems to be sending the full filename (i.e. including full path)
88             # of the template, while we only need the relative path, so let's
89             # strip the base path from the template filename
90             foreach (@{$self->{engine}->{path}}) {
91             my $basepath = $_;
92             $basepath .= '/' unless $basepath =~ m!/^!;
93              
94             next unless $template =~ m/^$basepath/;
95              
96             $template =~ s/^$basepath//;
97             }
98              
99             # ignore 'bad' tokens - this is here because for some reason
100             # Dancer is passing the entire user agent as a token, and I can't
101             # find the cause of that yet.
102             foreach (keys %$tokens) {
103             delete $tokens->{$_} if m/[ ()]/;
104             }
105              
106             my $output = try { $self->{engine}->render($template, $tokens) } catch { croak $_ };
107             return $output;
108             }
109              
110             =head1 SEE ALSO
111              
112             L, L
113              
114             =head1 AUTHOR
115              
116             Ido Perlmuter, C<< >>
117              
118             =head1 ACKNOWLEDGEMENTS
119              
120             =over
121              
122             =item * Alexis Sukrieh, C<< >>
123              
124             Author of L, who wrote L,
125             on which this module is based.
126              
127             =item * Sawyer X, C<< >>
128              
129             Submitted helpful changes for version 0.3.
130              
131             =item * Franck Cuny C<< >>
132              
133             Submitted a simple test for version 0.4.
134              
135             =back
136              
137             =head1 TODO
138              
139             =over 2
140              
141             =item * Non-file sources
142              
143             Find a way to allow using templates from other source, such as
144             a database, just like in L.
145              
146             =item * Fine-tune Tenjin
147              
148             Allow passing arguments to Tenjin, such as USE_STRICT.
149              
150             =back
151              
152             =head1 BUGS
153              
154             Please report any bugs or feature requests to C, or through
155             the web interface at L. I will be notified, and then you'll
156             automatically be notified of progress on your bug as I make changes.
157              
158             =head1 SUPPORT
159              
160             You can find documentation for this module with the perldoc command.
161              
162             perldoc Dancer::Template::Tenjin
163              
164             You can also look for information at:
165              
166             =over 4
167              
168             =item * RT: CPAN's request tracker
169              
170             L
171              
172             =item * AnnoCPAN: Annotated CPAN documentation
173              
174             L
175              
176             =item * CPAN Ratings
177              
178             L
179              
180             =item * Search CPAN
181              
182             L
183              
184             =back
185              
186             =head1 LICENSE AND COPYRIGHT
187              
188             Copyright 2010-2011 Ido Perlmuter.
189              
190             This program is free software; you can redistribute it and/or modify it
191             under the terms of either: the GNU General Public License as published
192             by the Free Software Foundation; or the Artistic License.
193              
194             See http://dev.perl.org/licenses/ for more information.
195              
196             =cut
197              
198             1;