File Coverage

blib/lib/Prancer/Plugin/Xslate.pm
Criterion Covered Total %
statement 67 76 88.1
branch 5 28 17.8
condition 4 35 11.4
subroutine 16 20 80.0
pod 5 6 83.3
total 97 165 58.7


line stmt bran cond sub pod time code
1             package Prancer::Plugin::Xslate;
2              
3 2     2   60096 use strict;
  2         4  
  2         88  
4 2     2   10 use warnings FATAL => 'all';
  2         4  
  2         176  
5              
6 2     2   790 use version;
  2         2387  
  2         12  
7             our $VERSION = '1.01';
8              
9 2     2   1248 use Prancer::Plugin;
  2         26807  
  2         73  
10 2     2   15 use parent qw(Prancer::Plugin Exporter);
  2         5  
  2         9  
11              
12 2     2   126 use Prancer::Core;
  2         5  
  2         72  
13 2     2   1399 use Text::Xslate;
  2         18521  
  2         115  
14 2     2   17 use Try::Tiny;
  2         3  
  2         101  
15 2     2   11 use Carp;
  2         3  
  2         307  
16              
17             our @EXPORT_OK = qw(render mark_raw unmark_raw html_escape uri_escape);
18             our %EXPORT_TAGS = ('all' => [ @EXPORT_OK ]);
19              
20             # even though this *should* work automatically, it was not
21             our @CARP_NOT = qw(Prancer Try::Tiny);
22              
23             sub load {
24 7     7 0 219835 my $class = shift;
25 7         28 my $self = bless({}, $class);
26              
27             # the config is modified and used every time "render" is called
28 7   50     42 $self->{'_config'} = $self->config->get("template") || {};
29              
30             # now export the keyword with a reference to $self
31             {
32             ## no critic (ProhibitNoStrict ProhibitNoWarnings)
33 2     2   23 no strict 'refs';
  2         3  
  2         80  
  7         788  
34 2     2   7 no warnings 'redefine';
  2         2  
  2         1230  
35 7         14 *{"${\__PACKAGE__}::render"} = sub {
  7         127  
36 9 0 33 9   10470 my $this = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    50 0        
37             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
38             bless({}, shift) : bless({}, __PACKAGE__);
39 9         36 return $self->_render(@_);
40 7         51 };
41             }
42              
43 7         23 return $self;
44             }
45              
46             sub path {
47 3     3 1 93 my $self = shift;
48 3 50       12 if (@_) {
49 3         43 $self->{'_config'}->{'path'} = shift;
50             }
51 3         11 return $self->{'_config'}->{'path'};
52             }
53              
54             sub _render {
55 9     9   23 my ($self, $template, $vars, $config) = @_;
56              
57             # just pass all of the options directly to Text::Xslate
58             # some default options that are important to remember:
59             # cache = 1
60             # cache_dir = $ENV{'HOME'}/.xslate_cache
61             # verbose = 1
62             # suffix = '.tx'
63             # syntax = 'Kolon'
64             # type = 'html' (identical to xml)
65 9         73 my $tx_config = _merge($self->{'_config'}, $config);
66 9         20 my $tx = Text::Xslate->new(%{$tx_config});
  9         79  
67              
68             # merge configuration values into the template variable list
69 9         8946 my $user_config = $self->config->get();
70 9         390 $vars = _merge({ 'config' => $user_config }, $vars);
71              
72 9         120 return $tx->render($template, $vars);
73             }
74              
75             sub mark_raw {
76 0 0 0 0 1 0 my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    0 0        
77             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
78             bless({}, shift) : bless({}, __PACKAGE__);
79 0         0 return Text::Xslate::mark_raw(@_);
80             }
81              
82             sub unmark_raw {
83 0 0 0 0 1 0 my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    0 0        
84             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
85             bless({}, shift) : bless({}, __PACKAGE__);
86 0         0 return Text::Xslate::unmark_raw(@_);
87             }
88              
89             sub html_escape {
90 0 0 0 0 1 0 my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    0 0        
91             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
92             bless({}, shift) : bless({}, __PACKAGE__);
93 0         0 return Text::Xslate::html_escape(@_);
94             }
95              
96             sub uri_escape {
97 0 0 0 0 1 0 my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    0 0        
98             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
99             bless({}, shift) : bless({}, __PACKAGE__);
100 0         0 return Text::Xslate::uri_escape(@_);
101             }
102              
103             # stolen from Hash::Merge::Simple
104             sub _merge {
105 18     18   37 my ($left, @right) = @_;
106              
107 18 50       53 return $left unless @right;
108 18 50       50 return _merge($left, _merge(@right)) if @right > 1;
109              
110 18         28 my ($right) = @right;
111 18         26 my %merged = %{$left};
  18         79  
112              
113 18         27 for my $key (keys %{$right}) {
  18         62  
114 10         23 my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left;
  20         62  
115              
116 10 50 66     39 if ($hr and $hl) {
117 0         0 $merged{$key} = _merge($left->{$key}, $right->{$key});
118             } else {
119 10         35 $merged{$key} = $right->{$key};
120             }
121             }
122              
123 18         71 return \%merged;
124             }
125              
126             1;
127              
128             =head1 NAME
129              
130             Prancer::Plugin::Xslate
131              
132             =head1 SYNOPSIS
133              
134             This plugin provides access to the L templating engine for your
135             L application and exports a keyword to access the configured engine.
136              
137             This template plugin supports setting the basic configuration in your Prancer
138             application's configuration file. You can also configure all options at runtime
139             using arguments to C.
140              
141             To set a configuration in your application's configuration file, begin the
142             configuration block with C