File Coverage

blib/lib/Dancer2/Template/Mason.pm
Criterion Covered Total %
statement 19 20 95.0
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 24 27 88.8


line stmt bran cond sub pod time code
1             package Dancer2::Template::Mason;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Mason wrapper for Dancer2
4             $Dancer2::Template::Mason::VERSION = '0.1.1';
5 1     1   246895 use strict;
  1         9  
  1         30  
6 1     1   16 use warnings;
  1         1  
  1         24  
7              
8 1     1   705 use HTML::Mason::Interp;
  1         105573  
  1         95  
9              
10 1     1   801 use Moo;
  1         7123  
  1         4  
11              
12             require FindBin;
13              
14             with 'Dancer2::Core::Role::Template';
15              
16             has _engine => (
17             is => 'ro',
18             lazy => 1,
19             default => sub {
20             my %config = %{$_[0]->config || {}};
21              
22             delete @config{qw/ environment location extension /};
23              
24             HTML::Mason::Interp->new( %config );
25             },
26             );
27              
28             has _root_dir => (
29             is => 'rw',
30             lazy => 1,
31             default => sub {
32             my $self = shift;
33              
34             $self->config->{comp_root} ||=
35             $self->settings->{views} || $FindBin::Bin . '/views';
36             },
37             );
38              
39 0     0   0 sub _build_name { 'Dancer2::Template::Mason' }
40              
41             has '+default_tmpl_ext' => (
42             is => 'ro',
43             lazy => 1,
44             default => sub {
45             $_[0]->config->{extension} || 'mason';
46             },
47             );
48              
49             sub render {
50 1     1 0 16803 my ($self, $template, $tokens) = @_;
51              
52 1         26 my $root_dir = $self->_root_dir;
53            
54 1         80 $template =~ s/^\Q$root_dir//; # cut the leading path
55              
56 1         2 my $content;
57 1         22 $self->_engine->out_method( \$content );
58 1         33979 $self->_engine->exec($template, %$tokens);
59 1         5743 return $content;
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             Dancer2::Template::Mason - Mason wrapper for Dancer2
73              
74             =head1 VERSION
75              
76             version 0.1.1
77              
78             =head1 SYNOPSIS
79              
80             # in 'config.yml'
81             template: 'mason'
82              
83             # in the app
84            
85             get '/foo', sub {
86             template 'foo' => {
87             title => 'bar'
88             };
89             };
90              
91             Then, on C<views/foo.mason>:
92              
93             <%args>
94             $title
95             </%args>
96              
97             <h1><% $title %></h1>
98              
99             <p>Mason says hi!</p>
100              
101             =head1 DESCRIPTION
102              
103             This class is an interface between Dancer's template engine abstraction layer
104             and the L<HTML::Mason> templating system.
105             For templates using L<Mason> version
106             2.x, what you want is L<Dancer2::Template::Mason2>.
107              
108             In order to use this engine, set the template to 'mason' in the configuration
109             file:
110              
111             template: mason
112              
113             =head1 HTML::Mason::Interp CONFIGURATION
114              
115             Parameters can also be passed to the L<HTML::Mason::Interp> interpreter via
116             the configuration file, like so:
117              
118             engines:
119             mason:
120             default_escape_flags: ['h']
121              
122             If unspecified, C<comp_root> defaults to the C<views> configuration setting
123             or, if it's undefined, to the C</views> subdirectory of the application.
124              
125             =head2 Notes on Mason Caching and Performance
126              
127             To improve performance of your templates, Mason creates a long-term cache on
128             disk. This is great in production, where you want to squeak every ounce of
129             performance out of your application, but in development, it can be a pain
130             to constantly clear the cache. And when developing, it's not always clear
131             where Mason even stores the cache!
132              
133             For development, we recommend disabling the Mason cache. In your
134             F<environments/development.yml> file, you'd put the following:
135              
136             template: "mason"
137             engines:
138             template:
139             mason:
140             use_object_files: 0
141             static_source: 0
142              
143             (static_source is also a potential performance enhancing setting.
144             See L<the Mason docs|https://metacpan.org/dist/HTML-Mason/view/lib/HTML/Mason/Admin.pod#Static-Source-Mode>
145             for more details)
146              
147             In production (F<environments/production.yml>), recommended settings are:
148              
149             template: "mason"
150             engines:
151             template:
152             mason:
153             extension: m
154             data_dir: "/path/to/your/app/var/"
155             use_object_files: 1
156             static_source: 1
157              
158             C<data_dir> tells Mason where to store its long-term cache. It must be
159             an absolute path.
160              
161             Clearing the cache is as easy as:
162              
163             rm -rf /path/to/your/app/var/obj
164              
165             See L<the Mason docs|https://metacpan.org/dist/HTML-Mason/view/lib/HTML/Mason/Admin.pod#Object-Files>
166             for more information on the object files and caching.
167              
168             =head1 SEE ALSO
169              
170             L<Dancer2>, L<HTML::Mason>.
171              
172             For Mason v2, see L<Mason> and L<Dancer2::Template::Mason2>.
173              
174             And, of course, there is the original L<Dancer::Template::Mason>.
175              
176             =head1 AUTHOR
177              
178             Yanick Champoux <yanick@cpan.org>
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             This software is copyright (c) 2023 by Yanick Champoux.
183              
184             This is free software; you can redistribute it and/or modify it under
185             the same terms as the Perl 5 programming language system itself.
186              
187             =cut