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.0';
5 1     1   245212 use strict;
  1         13  
  1         30  
6 1     1   6 use warnings;
  1         3  
  1         25  
7              
8 1     1   646 use HTML::Mason::Interp;
  1         103316  
  1         92  
9              
10 1     1   766 use Moo;
  1         6909  
  1         10  
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 15850 my ($self, $template, $tokens) = @_;
51              
52 1         26 my $root_dir = $self->_root_dir;
53            
54 1         79 $template =~ s/^\Q$root_dir//; # cut the leading path
55              
56 1         3 my $content;
57 1         20 $self->_engine->out_method( \$content );
58 1         31898 $self->_engine->exec($template, %$tokens);
59 1         5355 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.0
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             =head1 SEE ALSO
126              
127             L<Dancer2>, L<HTML::Mason>.
128              
129             For Mason v2, see L<Mason> and L<Dancer2::Template::Mason2>.
130              
131             And, of course, there is the original L<Dancer::Template::Mason>.
132              
133             =head1 AUTHOR
134              
135             Yanick Champoux <yanick@cpan.org>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2023 by Yanick Champoux.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut