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   113366 use strict;
  2         5  
  2         94  
4 2     2   12 use warnings FATAL => 'all';
  2         3  
  2         110  
5              
6 2     2   438 use version;
  2         1824  
  2         19  
7             our $VERSION = '1.00';
8              
9 2     2   1550 use Prancer::Plugin;
  2         26700  
  2         88  
10 2     2   21 use parent qw(Prancer::Plugin Exporter);
  2         4  
  2         13  
11              
12 2     2   160 use Prancer::Core;
  2         3  
  2         93  
13 2     2   2074 use Text::Xslate;
  2         37973  
  2         165  
14 2     2   22 use Try::Tiny;
  2         4  
  2         121  
15 2     2   10 use Carp;
  2         3  
  2         347  
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 222349 my $class = shift;
25 7         24 my $self = bless({}, $class);
26              
27             # the config is modified and used every time "render" is called
28 7   50     48 $self->{'_config'} = $self->config->remove("template") || {};
29              
30             # now export the keyword with a reference to $self
31             {
32             ## no critic (ProhibitNoStrict ProhibitNoWarnings)
33 2     2   83 no strict 'refs';
  2         3  
  2         92  
  7         922  
34 2     2   10 no warnings 'redefine';
  2         2  
  2         1656  
35 7         11 *{"${\__PACKAGE__}::render"} = sub {
  7         132  
36 9 0 33 9   10752 my $this = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
    50 0        
37             shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
38             bless({}, shift) : bless({}, __PACKAGE__);
39 9         44 return $self->_render(@_);
40 7         51 };
41             }
42              
43 7         26 return $self;
44             }
45              
46             sub path {
47 3     3 1 21 my $self = shift;
48 3 50       12 if (@_) {
49 3         44 $self->{'_config'}->{'path'} = shift;
50             }
51 3         11 return $self->{'_config'}->{'path'};
52             }
53              
54             sub _render {
55 9     9   22 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         84 my $tx_config = _merge($self->{'_config'}, $config);
66 9         16 my $tx = Text::Xslate->new(%{$tx_config});
  9         94  
67              
68             # merge configuration values into the template variable list
69 9         9716 my $user_config = $self->config->get();
70 9         456 $vars = _merge({ 'config' => $user_config }, $vars);
71              
72 9         123 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   43 my ($left, @right) = @_;
106              
107 18 50       63 return $left unless @right;
108 18 50       52 return _merge($left, _merge(@right)) if @right > 1;
109              
110 18         31 my ($right) = @right;
111 18         22 my %merged = %{$left};
  18         93  
112              
113 18         31 for my $key (keys %{$right}) {
  18         69  
114 10         28 my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left;
  20         65  
115              
116 10 50 66     46 if ($hr and $hl) {
117 0         0 $merged{$key} = _merge($left->{$key}, $right->{$key});
118             } else {
119 10         40 $merged{$key} = $right->{$key};
120             }
121             }
122              
123 18         85 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