File Coverage

blib/lib/Dancer/Template/Mason.pm
Criterion Covered Total %
statement 20 21 95.2
branch n/a
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 27 29 93.1


line stmt bran cond sub pod time code
1             package Dancer::Template::Mason;
2             BEGIN {
3 3     3   395926 $Dancer::Template::Mason::AUTHORITY = 'cpan:YANICK';
4             }
5             # ABSTRACT: Mason wrapper for Dancer
6             $Dancer::Template::Mason::VERSION = '0.004001';
7 3     3   28 use strict;
  3         7  
  3         90  
8 3     3   14 use warnings;
  3         6  
  3         82  
9              
10 3     3   3238 use HTML::Mason::Interp;
  3         305308  
  3         327  
11              
12             require Dancer;
13              
14 3     3   5030 use Moo;
  3         57182  
  3         29  
15              
16             require FindBin;
17             require Dancer::Config;
18              
19             Dancer::Config->import( 'setting' );
20              
21             extends 'Dancer::Template::Abstract';
22              
23             has _engine => (
24             is => 'ro',
25             lazy => 1,
26             default => sub {
27             my %config = %{$_[0]->config || {}};
28              
29             delete $config{$_} for qw/ environment location extension /;
30             HTML::Mason::Interp->new( %config );
31             },
32             );
33              
34             has _root_dir => (
35             is => 'rw',
36             lazy => 1,
37             default => sub {
38             $_[0]->config->{comp_root} ||=
39             setting( 'views' ) || $FindBin::Bin . '/views';
40             },
41             );
42              
43 0     0   0 sub _build_name { 'Dancer::Template::Mason' }
44              
45             has default_tmpl_ext => (
46             is => 'ro',
47             lazy => 1,
48             default => sub {
49             $_[0]->config->{extension} || 'mason';
50             },
51             );
52              
53             sub render {
54 2     2 1 5734 my ($self, $template, $tokens) = @_;
55              
56 2         11 my $root_dir = $self->_root_dir;
57            
58 2         112 $template =~ s/^\Q$root_dir//; # cut the leading path
59              
60 2         6 my $content;
61 2         12 $self->_engine->out_method( \$content );
62 2         69101 $self->_engine->exec($template, %$tokens);
63 2         10767 return $content;
64             }
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Dancer::Template::Mason - Mason wrapper for Dancer
77              
78             =head1 VERSION
79              
80             version 0.004001
81              
82             =head1 SYNOPSIS
83              
84             # in 'config.yml'
85             template: 'mason'
86              
87             # in the app
88            
89             get '/foo', sub {
90             template 'foo' => {
91             title => 'bar'
92             };
93             };
94              
95             Then, on C<views/foo.mason>:
96              
97             <%args>
98             $title
99             </%args>
100              
101             <h1><% $title %></h1>
102              
103             <p>Mason says hi!</p>
104              
105             =head1 DESCRIPTION
106              
107             This class is an interface between Dancer's template engine abstraction layer
108             and the L<HTML::Mason> templating system.
109             For templates using L<Mason> version
110             2.x, what you want is L<Dancer::Template::Mason2>.
111              
112             In order to use this engine, set the template to 'mason' in the configuration
113             file:
114              
115             template: mason
116              
117             =head1 HTML::Mason::Interp CONFIGURATION
118              
119             Parameters can also be passed to the L<HTML::Mason::Interp> interpreter via
120             the configuration file, like so:
121              
122             engines:
123             mason:
124             default_escape_flags: ['h']
125              
126             If unspecified, C<comp_root> defaults to the C<views> configuration setting
127             or, if it's undefined, to the C</views> subdirectory of the application.
128              
129             =head1 SEE ALSO
130              
131             L<Dancer>, L<HTML::Mason>.
132              
133             For Mason v2, see L<Mason> and L<Dancer::Template::Mason2>.
134              
135             =head1 AUTHOR
136              
137             Yanick Champoux <yanick@cpan.org>
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             This software is copyright (c) 2014 by Yanick Champoux.
142              
143             This is free software; you can redistribute it and/or modify it under
144             the same terms as the Perl 5 programming language system itself.
145              
146             =cut