File Coverage

blib/lib/Dancer/Template/Mason2.pm
Criterion Covered Total %
statement 32 39 82.0
branch 1 2 50.0
condition 4 14 28.5
subroutine 9 12 75.0
pod 5 5 100.0
total 51 72 70.8


line stmt bran cond sub pod time code
1             package Dancer::Template::Mason2;
2             $Dancer::Template::Mason2::VERSION = '0.05';
3 2     2   18313 use Dancer::Config 'setting';
  2         143362  
  2         121  
4 2     2   14 use File::Basename;
  2         4  
  2         109  
5 2     2   457 use FindBin;
  2         754  
  2         58  
6 2     2   800 use Mason;
  2         1703856  
  2         59  
7 2     2   14 use strict;
  2         2  
  2         45  
8 2     2   6 use warnings;
  2         2  
  2         37  
9 2     2   7 use base 'Dancer::Template::Abstract';
  2         5  
  2         755  
10              
11             my $_engine;
12             my $root_dir;
13              
14             sub init {
15 1     1 1 32 my $self = shift;
16 1   50     7 my $config = $self->config || {};
17              
18 1   33     24 $root_dir = $config->{comp_root} ||= setting('views') || $FindBin::Bin . '/views';
      33        
19 1   33     85 $config->{data_dir} ||= dirname($root_dir) . "/data";
20 1 50       5 $config->{autoextend_request_path} = 0 if !exists( $config->{autoextend_request_path} );
21              
22 1         10 $_engine = Mason->new(%$config);
23             }
24              
25 0     0 1 0 sub default_tmpl_ext { "mc" }
26              
27             sub render {
28 1     1 1 74028 my ( $self, $template, $tokens ) = @_;
29              
30 1         29 $template =~ s/^\Q$root_dir//; # cut the leading path
31 1         3 $template =~ y|\\|/|; # convert slashes on Windows
32              
33 1         8 my $content = $_engine->run( $template, %$tokens )->output;
34 1         256182 return $content;
35             }
36              
37             sub view {
38 0     0 1   my ( $self, $view ) = @_;
39              
40 0           my $views_dir = Dancer::App->current->setting('views');
41 0   0       my $tmpl_ext = $self->config->{extension} || $self->default_tmpl_ext();
42 0           my $view_path = sprintf( '%s/%s.%s', $views_dir, $view, $self->default_tmpl_ext );
43 0           return $view_path;
44             }
45              
46 0     0 1   sub view_exists { return 1; }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =head1 NAME
55              
56             Dancer::Template::Mason2 - Mason 2.x wrapper for Dancer
57              
58             =head1 VERSION
59              
60             version 0.05
61              
62             =head1 SYNOPSIS
63              
64             # in 'config.yml'
65             template: 'mason2'
66              
67             # in the app
68            
69             get '/foo', sub {
70             template 'foo' => {
71             title => 'bar'
72             };
73             };
74              
75             Then, in C<views/foo.mc>:
76              
77             <%args>
78             $.title
79             </%args>
80              
81             <h1><% $.title %></h1>
82              
83             <p>Mason says hi!</p>
84              
85             =head1 DESCRIPTION
86              
87             This class is an interface between Dancer's template engine abstraction layer
88             and the L<Mason 2.x|Mason> templating system.
89              
90             In order to use this engine, set the template to 'mason2' in the configuration
91             file:
92              
93             template: mason2
94              
95             The default template extension is ".mc".
96              
97             =head1 CONFIGURATION
98              
99             Parameters can also be passed to C<< Mason->new >> via the configuration file,
100             like so:
101              
102             engines:
103             mason2:
104             data_dir: /path/to/data_dir
105              
106             C<comp_root>, if not specified, defaults to the C<views> configuration setting
107             or, if it's undefined, to the C</views> subdirectory of the application.
108              
109             C<data_dir>, if not specified, defaults to a C</data> subdirectory alongside
110             the C<comp_root>.
111              
112             =head1 SEE ALSO
113              
114             L<Dancer>, L<Mason>, L<Dancer::Template::Mason>
115              
116             =head1 AUTHOR
117              
118             Jonathan Swartz <swartz@pobox.com>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2011 by Jonathan Swartz.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut