File Coverage

blib/lib/Dotiac/DTL/Template.pm
Criterion Covered Total %
statement 60 72 83.3
branch 5 12 41.6
condition 7 10 70.0
subroutine 7 9 77.7
pod 6 6 100.0
total 85 109 77.9


line stmt bran cond sub pod time code
1             ###############################################################################
2             #Template.pm
3             #Last Change: 2009-01-19
4             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
5             #Version 0.8
6             ####################
7             #This file is part of the Dotiac::DTL project.
8             #http://search.cpan.org/perldoc?Dotiac::DTL
9             #
10             #Template.pm is published under the terms of the MIT license, which basically
11             #means "Do with it whatever you want". For more information, see the
12             #license.txt file that should be enclosed with libsofu distributions. A copy of
13             #the license is (at the time of writing) also available at
14             #http://www.opensource.org/licenses/mit-license.php .
15             ###############################################################################
16            
17             package Dotiac::DTL::Template;
18             require Dotiac::DTL::Core;
19             require Dotiac::DTL::Addon;
20            
21             our $VERSION = 0.8;
22            
23 12     12   71 use strict;
  12         23  
  12         470  
24 12     12   70 use warnings;
  12         22  
  12         387  
25 12     12   103 use Carp;
  12         27  
  12         9019  
26            
27             sub new {
28 296     296 1 638 my $class=shift;
29 296         725 my $self={};
30 296         762 bless $self,$class;
31 296         1039 $self->{vars}={};
32 296         699 $self->{first}=shift;
33 296   66     1280 $self->{currentdir}=shift(@_) || $Dotiac::DTL::CURRENTDIR;
34 296   66     999 $self->{parser}=shift(@_) || $Dotiac::DTL::PARSER;
35 296 100       910 if (@_) {
36             #$self->{params}=[keys %{shift(@_)}];
37 295         477 $self->{params}=[grep {substr($_,0,1) ne "`"} sort keys %{shift(@_)}];
  985         3083  
  295         1783  
38             }
39             else {
40             #$self->{params}=[keys %Dotiac::DTL::params];
41 1         3 $self->{params}=[grep {substr($_,0,1) ne "`"} sort keys %Dotiac::DTL::params];
  1         6  
42             }
43 296         1501 return $self;
44             }
45            
46             sub param {
47 5     5 1 1159 my $self=shift;
48 5         45 my $name="";
49 5 50       18 return @{$self->{params}} unless @_;
  5         45  
50 0         0 while (@_) {
51 0         0 $name=shift;
52 0 0       0 $self->{vars}->{$name}=shift if @_;
53             }
54 0 0       0 return $self->{vars}->{$name} if $name;
55            
56             }
57            
58             sub string {
59 255     255 1 144122 my $self=shift;
60 255   100     1054 my $vars=shift || {};
61 255         612 %Dotiac::DTL::blocks=();
62 255         522 %Dotiac::DTL::cycle=();
63 255         444 %Dotiac::DTL::globals=();
64 255         585 $Dotiac::DTL::currentdir=$self->{currentdir};
65 255         437 my $p=$Dotiac::DTL::PARSER;
66 255         474 $Dotiac::DTL::PARSER=$self->{parser};
67 255         406 my $ret="";
68             eval {
69 255         718 $ret=$self->{first}->string({%{$self->{vars}},%{$vars}},$Dotiac::DTL::AUTOESCAPING);
  255         757  
  255         2058  
70 255         2362 1;
71 255 50       473 } or do {
72 0         0 croak "Error while rendering output to string\n $@\n.";
73 0         0 undef $@;
74             };
75 255         848 Dotiac::DTL::Addon::restore();
76 255         420 $Dotiac::DTL::PARSER=$p;
77 255         1290 return $ret;
78             }
79            
80             sub render {
81 0     0 1 0 my $self=shift;
82 0         0 return $self->string(@_);
83             }
84             sub output {
85 0     0 1 0 my $self=shift;
86 0         0 return $self->string(@_);
87             }
88            
89             sub print {
90 249     249 1 229812 my $self=shift;
91 249   50     1014 my $vars=shift || {};
92 249         757 %Dotiac::DTL::blocks=();
93 249         649 %Dotiac::DTL::cycle=();
94 249         417 %Dotiac::DTL::globals=();
95 249         685 $Dotiac::DTL::currentdir=$self->{currentdir};
96 249         420 my $p=$Dotiac::DTL::PARSER;
97 249         637 $Dotiac::DTL::PARSER=$self->{parser};
98             eval {
99 249         592 $self->{first}->print({%{$self->{vars}},%{$vars}},$Dotiac::DTL::AUTOESCAPING);
  249         590  
  249         1856  
100 249         2015 1;
101 249 50       489 } or do {
102 0         0 croak "Error while printing output\n $@\n.";
103 0         0 undef $@;
104             };
105 249         921 Dotiac::DTL::Addon::restore();
106 249         422 $Dotiac::DTL::PARSER=$p;
107 249         724 return;
108             }
109            
110             1;
111            
112             __END__