File Coverage

blib/lib/Dotiac/DTL/Tag/extends.pm
Criterion Covered Total %
statement 110 123 89.4
branch 27 40 67.5
condition 4 9 44.4
subroutine 12 14 85.7
pod 11 11 100.0
total 164 197 83.2


line stmt bran cond sub pod time code
1             #extends.pm
2             #Last Change: 2009-01-19
3             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
4             #Version 0.8
5             ####################
6             #This file is part of the Dotiac::DTL project.
7             #http://search.cpan.org/perldoc?Dotiac::DTL
8             #
9             #extends.pm is published under the terms of the MIT license, which basically
10             #means "Do with it whatever you want". For more information, see the
11             #license.txt file that should be enclosed with libsofu distributions. A copy of
12             #the license is (at the time of writing) also available at
13             #http://www.opensource.org/licenses/mit-license.php .
14             ###############################################################################
15            
16             package Dotiac::DTL::Tag::extends;
17 11     11   60 use base qw/Dotiac::DTL::Tag/;
  11         20  
  11         860  
18 11     11   63 use strict;
  11         24  
  11         473  
19 11     11   56 use warnings;
  11         25  
  11         24334  
20             require Scalar::Util;
21            
22             our $VERSION = 0.8;
23            
24             sub new {
25 2     2 1 5 my $class=shift;
26 2         7 my $self={p=>shift()};
27 2         4 my $name=shift;
28 2 50       7 die "This extend needs a filename or variable" unless $name;
29 2         12 $name=(Dotiac::DTL::get_variables($name))[0];
30 2         7 my $f=substr $name,0,1;
31 2         4 my $e=substr $name,-1,1;
32 2 100 66     14 if ($f eq "`" and $e eq "`") {
33 1         7 $self->{load}=Dotiac::DTL::devar($name,{},0);
34 1         8 my $tem = Dotiac::DTL->safenew($self->{load});
35 1         6 $self->{content}=$tem->{first};
36             }
37             else {
38 1         4 $self->{var}=$name;
39             }
40 2         5 my $obj=shift;
41 2         5 my $data=shift;
42 2         3 my $pos=shift;
43 2         4 my $found="";
44 2         12 $self->{data}=$obj->parse($data,$pos,\$found,"endextends");
45 2         7 bless $self,$class;
46 2         8 return $self;
47             }
48             sub print {
49 5     5 1 9 my $self=shift;
50 5         16 print $self->{p};
51 5         22 $self->{data}->eval(@_);
52             #die Data::Dumper->Dump([$self]);
53 5 100       14 if ($self->{content}) {
54 1         7 $self->{content}->print(@_);
55             }
56             else {
57 4         16 my $tem = Dotiac::DTL::devar_raw($self->{var},@_);
58 4 100 33     15 if ($tem->scalar()) {
    50          
59 2         6 $tem = Dotiac::DTL->safenew($tem->repr);
60             }
61             elsif ($tem->object() and $tem->content->isa("Dotiac::DTL::Template")) {
62 2         7 $tem=$tem->content;
63             }
64             else {
65 0         0 die "Can't extend with \"$tem\"";
66             }
67 4         25 $tem->{first}->print(@_)
68             }
69 5 50       40 $self->{n}->print(@_) if $self->{n};
70             }
71             sub string {
72 5     5 1 10 my $self=shift;
73 5         31 $self->{data}->eval(@_);
74 5 100       17 if ($self->{content}) {
75 1 50       7 return $self->{p}.$self->{content}->string(@_).($self->{n}?$self->{n}->string(@_):"");
76             }
77 4         29 my $tem = Dotiac::DTL::devar_raw($self->{var},@_);
78 4 100 33     19 if ($tem->scalar()) {
    50          
79 2         12 $tem = Dotiac::DTL->safenew($tem->repr);
80             }
81             elsif ($tem->object() and $tem->content->isa("Dotiac::DTL::Template")) {
82 2         8 $tem=$tem->content;
83             }
84             else {
85 0         0 die "Can't extend with \"$tem\"";
86             }
87 4 50       32 return $self->{p}.$tem->{first}->string(@_).($self->{n}?$self->{n}->string(@_):"");
88            
89             }
90             sub perl {
91 5     5 1 13 my $self=shift;
92 5         9 my $fh=shift;
93 5         12 my $id=shift;
94 5         43 $self->SUPER::perl($fh,$id,@_);
95 5 100       19 if ($self->{load}) {
96 1         9 print $fh "my \$template$id=Dotiac::DTL->safenew(\"".quotemeta($self->{load})."\");\n";
97             }
98             else {
99 4         11 print $fh "my ";
100 4         26 print $fh (Data::Dumper->Dump([$self->{var}],["\$name$id"]));
101             }
102 5         189 $id=$self->{data}->perl($fh,$id+1,@_);
103 5 50       37 return $self->{n}->perl($fh,$id+1,@_) if $self->{n};
104 0         0 return $id;
105             }
106             sub perlprint {
107 5     5 1 10 my $self=shift;
108 5         8 my $fh=shift;
109 5         7 my $id=shift;
110 5         6 my $level=shift;
111 5         23 $self->SUPER::perlprint($fh,$id,$level,@_);
112 5         11 my $in="\t" x $level;
113 5         27 my $nid = $self->{data}->perleval($fh,$id+1,$level,@_);
114 5 100       16 if ($self->{content}) {
115 1         5 print $fh "$in\$template$id"."->{first}->print(\$vars,\$escape,\@_);\n";
116             }
117             else {
118 4         17 print $fh $in,"my \$template$id = Dotiac::DTL::devar_raw(\$name$id,\$vars,\$escape,\@_);\n";
119 4         11 print $fh $in,"if (\$template$id->scalar()) {\n";
120 4         12 print $fh $in,"\t\$template$id = Dotiac::DTL->safenew(\$template$id->repr());\n";
121 4         13 print $fh $in,"} elsif (\$template$id->object() and \$template$id->content->isa(\"Dotiac::DTL::Template\")) {\n";
122 4         11 print $fh $in,"\t\$template$id=\$template$id->content;\n";
123 4         8 print $fh $in,"} else {\n";
124 4         11 print $fh $in,"\tdie \"Can't extend with \\\"\$template$id\\\"\";\n";
125 4         8 print $fh $in,"}\n";
126 4         12 print $fh $in,"\$template$id"."->{first}->print(\$vars,\$escape,\@_);\n";
127             }
128 5 50       34 return $self->{n}->perlprint($fh,$nid+1,$level,@_) if $self->{n};
129 0         0 return $nid;
130             }
131             sub perlstring {
132 5     5 1 11 my $self=shift;
133 5         8 my $fh=shift;
134 5         10 my $id=shift;
135 5         9 my $level=shift;
136 5         21 $self->SUPER::perlstring($fh,$id,$level,@_);
137 5         11 my $in="\t" x $level;
138 5         27 my $nid = $self->{data}->perleval($fh,$id+1,$level,@_);
139 5 100       28 if ($self->{content}) {
140 1         5 print $fh "$in\$r.=\$template$id"."->{first}->string(\$vars,\$escape,\@_);\n";
141             }
142             else {
143 4         15 print $fh $in,"my \$template$id = Dotiac::DTL::devar_raw(\$name$id,\$vars,\$escape,\@_);\n";
144 4         23 print $fh $in,"if (\$template$id->scalar()) {\n";
145 4         12 print $fh $in,"\t\$template$id = Dotiac::DTL->safenew(\$template$id->repr());\n";
146 4         17 print $fh $in,"} elsif (\$template$id->object() and \$template$id->content->isa(\"Dotiac::DTL::Template\")) {\n";
147 4         10 print $fh $in,"\t\$template$id=\$template$id->content;\n";
148 4         8 print $fh $in,"} else {\n";
149 4         9 print $fh $in,"\tdie \"Can't extend with \\\"\$template$id\\\"\";\n";
150 4         7 print $fh $in,"}\n";
151 4         12 print $fh $in,"\$r.=\$template$id"."->{first}->string(\$vars,\$escape,\@_);\n";
152             }
153 5 50       33 return $self->{n}->perlstring($fh,$nid+1,$level,@_) if $self->{n};
154 0         0 return $nid;
155             }
156             sub perlcount {
157 0     0 1 0 my $self=shift;
158 0         0 my $id=shift;
159 0         0 $id = $self->{data}->perlcount($id+1);
160 0 0       0 return $id unless $self->{n};
161 0         0 return $self->{n}->perlcount($id+1);
162             }
163             sub perleval {
164 5     5 1 11 my $self=shift;
165 5         7 my $fh=shift;
166 5         9 my $id=shift;
167 5         22 $id=$self->{data}->perleval($fh,$id+1,@_);
168 5 50       18 return $id unless $self->{n};
169 5         23 return $self->{n}->perleval($fh,$id+1,@_);
170             }
171             sub perlinit {
172 5     5 1 12 my $self=shift;
173 5         10 my $fh=shift;
174 5         9 my $id=shift;
175 5         28 $id=$self->{data}->perlinit($fh,$id+1,@_);
176 5 50       20 return $id unless $self->{n};
177 5         23 return $self->{n}->perlinit($fh,$id+1,@_);
178             }
179             sub next {
180 2     2 1 5 my $self=shift;
181 2         15 $self->{n}=shift;
182             }
183             sub eval {
184 0     0 1   my $self=shift;
185 0           $self->{data}->eval(@_);
186 0           $self->{n}->eval(@_);
187             }
188             1;
189            
190             __END__