TemplateM/Simple.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 135 CC 12 SCAR 23.3
CC
12
Cov
0%
cast_loop
30.0
cast
12.0
cast_if
12.0
CRAP 10.3
1 package TemplateM::Simple; # $Id: Simple.pm 2 2013-04-02 10:51:49Z abalama $
2 use strict;
3
4 use base qw/Exporter/;
5 use vars qw($VERSION);
6 our $VERSION = 2.21;
7
8 use TemplateM::Util;
9 use Carp;
10
11 our @EXPORT = qw/html/;
12
13 sub cast {
14 my $self = shift;
15 my $hr = $_[0];
16
17 croak("[cast] Incorrect call of method \"cast\"") unless defined($hr);
18
19 unless (ref($hr) eq "HASH") {
20 $hr = {@_};
21 }
22
23 $self->{template}=~s/<!--\s*cgi:\s*(\S+?)\s*-->/_exec_directive($hr, $1)/ieg;
24 }
25 sub stash { goto &cast }
26 sub cast_loop {
27 my $self = shift;
28 my $name = shift || '';
29 my $ar = $_[0];
30
31 croak("[cast_loop] Incorrect call of method \"cast_loop\"") unless ($name);
32
33 if (ref($ar) eq "HASH") {
34 $ar=[$ar];
35 } else {
36 $ar = [{@_}] if ref($ar) ne "ARRAY";
37 }
38
39 my $pattern = '';
40 if ($self->{template} =~ m/<!--\s*do:\s*$name\s*-->(.*)<!--\s*loop:\s*$name\s*-->/s) {
41 $pattern = $1 || ''
42 }
43 my $pattern_copy = $pattern;
44 my $out;
45 foreach (@{$ar}) {
46 $pattern = $pattern_copy;
47 $pattern =~ s/<!--\s*val:\s*(\S+?)\s*-->/_exec_directive($_,$1)/ieg;
48 $out.=$pattern;
49 }
50
51 $self->{template} =~ s/(<!--\s*do:\s*$name\s*-->).*(<!--\s*loop:\s*$name\s*-->)/$out$1$pattern_copy$2/s;
52 }
53 sub loop { goto &cast_loop }
54 sub finalize {
55 my $self = shift;
56 my $name = shift;
57
58 croak("[finalize] Incorrect call of method \"finalize\"") unless ($name);
59
60 $self->{template} =~ s/<!--\s*do:\s*$name\s*-->.*<!--\s*loop:\s*$name\s*-->//s;
61 }
62 sub finish { goto &finalize }
63 sub cast_if {
64 my $self = shift;
65 my $name = shift;
66 my $predicate = shift || 0;
67 croak("[cast_if] Incorrect call of method \"cast_if\"") unless ($name);
68
69 if ($predicate) {
70 $self->{template} =~ s/<!--\s*if:\s*$name\s*-->(.*)<!--\s*end_?if:\s*$name\s*-->/$1/s;
71 $self->{template} =~ s/<!--\s*else:\s*$name\s*-->.*<!--\s*end_?else:\s*$name\s*-->//s;
72 } else {
73 $self->{template} =~ s/<!--\s*else:\s*$name\s*-->(.*)<!--\s*end_?else:\s*$name\s*-->/$1/s;
74 $self->{template} =~ s/<!--\s*if:\s*$name\s*-->.*<!--\s*end_?if:\s*$name\s*-->//s;
75 }
76 }
77 sub ifelse { goto &cast_if }
78 sub html {
79 my $self = shift;
80 my $header = $self->{header} || '';
81 ($header) = read_attributes([['HEAD','HEADER']],@_) if (defined $_[0]);
82 return $header.$self->{template};
83 }
84 sub output { goto &html }
85 sub _exec_directive {
86 my ($hr, $directive) = @_;
87
88 if (defined($hr->{$directive})) {
89 return $hr->{$directive};
90 } else {
91 return '';
92 }
93 }
94 1;
95