Mojo/Template.pm

filter: Statementstmt 99.3% 156 / 157 Branchbran 100% 84 / 84 Conditioncond 87.5% 77 / 88 MC/DCmcdc 64.4% 29 / 45 Subroutinesub 94.4% 17 / 18 Podpod 100% 4 / 4 totaltotal 92.6% 367 / 396 CC 74 SCAR 30.7
CC
74
Cov
96%
parse
33.1
_compile
28.1
_trim
5.0
CRAP 21.6
« Mojolicious/Command/Author/inflate.pm ↑ Summary Mojolicious/Routes/Match.pm »
1 package Mojo::Template;
2 21 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Mojo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Mojo::Util qw(decode encode monkey_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use constant DEBUG => $ENV{MOJO_TEMPLATE_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_TEMPLATE_DEBUG'} || 0
l!l
021
MC/DC0%0/1
$ENV{'MOJO_TEMPLATE_DEBUG'} || 0
$ENV{'MOJO_TEMPLATE_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_TEMPLATE_DEBUG'}
Aresult
11
00
11  
12 21 has [qw(append code prepend unparsed)] => '';
13 21 has [qw(auto_escape compiled vars)];
14 21 has capture_end => 'end';
15 21 has capture_start => 'begin';
16 21 has comment_mark => '#';
17 21 has encoding => 'UTF-8';
18 195 ▶has escape => sub { \&Mojo::Util::xml_escape };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 21 has [qw(escape_mark expression_mark trim_mark)] => '=';
20 21 has [qw(line_start replace_mark)] => '%';
21 21 has name => 'template';
22 21 has namespace => 'Mojo::Template::Sandbox';
23 21 has tag_start => '<%';
24 21 has tag_end => '%>';
25 0 ▶has tree => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26  
27 199 ▶sub parse {
Subroutine parse: called CC=33 SCAR=35.0
Pod documented
28 199 my ($self, $template) = @_;
29  
30 # Clean start
31 199 $self->unparsed($template)->tree(\my @tree)->compiled(undef);
32  
33 199 my $tag = $self->tag_start;
34 199 my $replace = $self->replace_mark;
35 199 my $expr = $self->expression_mark;
36 199 my $escp = $self->escape_mark;
37 199 my $cpen = $self->capture_end;
38 199 my $cmnt = $self->comment_mark;
39 199 my $cpst = $self->capture_start;
40 199 my $trim = $self->trim_mark;
41 199 my $end = $self->tag_end;
42 199 my $start = $self->line_start;
43  
44 199 my $line_re = qr/^(\s*)\Q$start\E(?:(\Q$replace\E)|(\Q$cmnt\E)|(\Q$expr\E))?(.*)$/;
45 199 my $token_re = qr/
46 (
47 \Q$tag\E(?:\Q$replace\E|\Q$cmnt\E) # Replace
48 |
49 \Q$tag$expr\E(?:\Q$escp\E)?(?:\s*\Q$cpen\E(?!\w))? # Expression
50 |
51 \Q$tag\E(?:\s*\Q$cpen\E(?!\w))? # Code
52 |
53 (?:(?<!\w)\Q$cpst\E\s*)?(?:\Q$trim\E)?\Q$end\E # End
54 )
55 /x;
56 199 my $cpen_re = qr/^\Q$tag\E(?:\Q$expr\E)?(?:\Q$escp\E)?\s*\Q$cpen\E(.*)$/;
57 199 my $end_re = qr/^(?:(\Q$cpst\E)\s*)?(\Q$trim\E)?\Q$end\E$/;
58  
59 # Split lines
60 199 my $op = 'text';
61 199 my ($trimming, $capture);
62 199 for my $line (split /\n/, $template) {
63  
64 # Turn Perl line into mixed line
65 1998 ▶ if ($op eq 'text' && $line =~ $line_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "text" and $line =~ /$line_re/u) 604 1394 1998
Condition100%3/3
$op eq "text" and $line =~ /$line_re/u
!ll&&!rl&&r
261368604
MC/DC100%2/2
$op eq "text" and $line =~ /$line_re/u
$op eq "text" $line =~ /$line_re/u
Truth table100%3/3
A and B
A= $op eq "text"B= $line =~ /$line_re/u
ABresult
0X0
100
111
66  
67 # Escaped start
68 604 ▶ if ($2) { $line = "$1$start$5" }
Branch: 2 branches
BranchTrueFalseTotal
if ($2) { } 9 595 604
elsif ($3) { } 3 592 595
69  
70 # Comment
71 3 elsif ($3) { $line = "$tag$3 $trim$end" }
72  
73 # Expression or code
74 592 ▶ else { $line = $4 ? "$1$tag$4$5 $end" : "$tag$5 $trim$end" }
Branch: 1 branch
BranchTrueFalseTotal
$4 ? : 221 371 592
75 }
76  
77 # Escaped line ending
78 1998 ▶ $line .= "\n" if $line !~ s/\\\\$/\\\n/ && $line !~ s/\\$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ s/\\\\$/\\\n/u or $line =~ s/\\$//u 1976 22 1998
Condition100%3/3
$line =~ s/\\\\$/\\\n/u or $line =~ s/\\$//u
l!l&&r!l&&!r
4181976
MC/DC100%2/2
$line =~ s/\\\\$/\\\n/u or $line =~ s/\\$//u
$line =~ s/\\\\$/\\\n/u $line =~ s/\\$//u
Truth table100%3/3
A or B
A= $line =~ s/\\\\$/\\\n/uB= $line =~ s/\\$//u
ABresult
1X1
011
000
79  
80 # Mixed line
81 1998 for my $token (split $token_re, $line) {
82  
83 # Capture end
84 5620 ▶ ($token, $capture) = ("$tag$1", 1) if $token =~ $cpen_re;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /$cpen_re/u 53 5567 5620
85  
86 # End
87 5620 ▶ if ($op ne 'text' && $token =~ $end_re) {
Branch: 6 branches
BranchTrueFalseTotal
if ($op ne 'text' and $token =~ /$end_re/u) { } 894 4726 5620
elsif ($token eq $tag) { } 451 4275 4726
elsif ($token eq "$tag$expr") { } 417 3858 4275
elsif ($token eq "$tag$expr$escp") { } 20 3838 3858
elsif ($token eq "$tag$cmnt") { } 6 3832 3838
elsif ($op ne 'cmnt') { } 3824 8 3832
Condition100%3/3
$op ne "text" and $token =~ /$end_re/u
!ll&&!rl&&r
3806920894
MC/DC100%2/2
$op ne "text" and $token =~ /$end_re/u
$op ne "text" $token =~ /$end_re/u
Truth table100%3/3
A and B
A= $op ne "text"B= $token =~ /$end_re/u
ABresult
0X0
100
111
88  
89 # Capture start
90 894 ▶ splice @tree, -1, 0, ['cpst'] if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 53 841 894
91  
92 # Trim left side
93 894 ▶ _trim(\@tree) if ($trimming = $2) && @tree > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $trimming = $2 and @tree > 1 426 468 894
Condition66%2/3
$trimming = $2 and @tree > 1
!ll&&!rl&&r
4680426
MC/DC50%1/2
$trimming = $2 and @tree > 1
$trimming = $2 @tree > 1
Truth table66%2/3
A and B
A= $trimming = $2B= @tree > 1
ABresult
0X0
100
111
94  
95 # Hint at end
96 894 push @tree, [$op = 'text', ''];
97 }
98  
99 # Code
100 451 elsif ($token eq $tag) { $op = 'code' }
101  
102 # Expression
103 417 elsif ($token eq "$tag$expr") { $op = 'expr' }
104  
105 # Expression that needs to be escaped
106 20 elsif ($token eq "$tag$expr$escp") { $op = 'escp' }
107  
108 # Comment
109 6 elsif ($token eq "$tag$cmnt") { $op = 'cmnt' }
110  
111 # Text (comments are just ignored)
112 elsif ($op ne 'cmnt') {
113  
114 # Replace
115 3824 ▶ $token = $tag if $token eq "$tag$replace";
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "$tag$replace" 13 3811 3824
116  
117 # Trim right side (convert whitespace to line noise)
118 3824 ▶ if ($trimming && $token =~ s/^(\s+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trimming and $token =~ s/^(\s+)//u) 425 3399 3824
Condition100%3/3
$trimming and $token =~ s/^(\s+)//u
!ll&&!rl&&r
33954425
MC/DC100%2/2
$trimming and $token =~ s/^(\s+)//u
$trimming $token =~ s/^(\s+)//u
Truth table100%3/3
A and B
A= $trimmingB= $token =~ s/^(\s+)//u
ABresult
0X0
100
111
119 425 push @tree, ['code', $1];
120 425 $trimming = 0;
121 }
122  
123 # Token (with optional capture end)
124 3824 ▶ push @tree, $capture ? ['cpen'] : (), [$op, $token];
Branch: 1 branch
BranchTrueFalseTotal
$capture ? : 53 3771 3824
125 3824 $capture = 0;
126 }
127 }
128  
129 # Optimize successive text lines separated by a newline
130 1998 ▶ push @tree, ['line'] and next
Branch: 1 branch
BranchTrueFalseTotal
if $tree[-4] and $tree[-4][0] ne "line" or (not $tree[-3] or $tree[-3][0] ne "text" or not $tree[-3][1] =~ /\n$/u) or ($tree[-2][0] ne "line" or $tree[-1][0] ne "text") 1176 822 1998
Condition82%19/23
($tree[-4] && $tree[-4][0] ne 'line' || (!$tree[-3] || $tree[-3][0] ne 'text' || !($tree[-3][1] =~ /\n$/u)) || ($tree[-2][0] ne 'line' || $tree[-1][0] ne 'text')) && (push(@tree, ['line']) && (next))
!ll&&!rl&&r
822115125
$tree[-4] and $tree[-4][0] ne "line" or (not $tree[-3] or $tree[-3][0] ne "text" or not $tree[-3][1] =~ /\n$/u) or ($tree[-2][0] ne "line" or $tree[-1][0] ne "text")
l!l&&r!l&&!r
11760822
$tree[-4] and $tree[-4][0] ne "line" or (not $tree[-3] or $tree[-3][0] ne "text" or not $tree[-3][1] =~ /\n$/u)
l!l&&r!l&&!r
110076822
$tree[-4] and $tree[-4][0] ne "line"
!ll&&!rl&&r
1377611100
not $tree[-3] or $tree[-3][0] ne "text" or not $tree[-3][1] =~ /\n$/u
l!l&&r!l&&!r
724822
not $tree[-3] or $tree[-3][0] ne "text"
l!l&&r!l&&!r
648826
$tree[-2][0] ne "line" or $tree[-1][0] ne "text"
l!l&&r!l&&!r
00822
push @tree, ["line"] and next
!ll
01176
MC/DC0%0/8
($tree[-4] && $tree[-4][0] ne 'line' || (!$tree[-3] || $tree[-3][0] ne 'text' || !($tree[-3][1] =~ /\n$/u)) || ($tree[-2][0] ne 'line' || $tree[-1][0] ne 'text')) && (push(@tree, ['line']) && (next))
$tree[-4] $tree[-4][0] ne "line" not $tree[-3] $tree[-3][0] ne "text" not $tree[-3][1] =~ /\n$/u $tree[-2][0] ne "line" $tree[-1][0] ne "text" push @tree, ["line"]
Truth table8%2/24
A and B or not(C) or D or not(E) or F or G && H
A= $tree[-4]B= $tree[-4][0] ne "line"C= not $tree[-3]D= $tree[-3][0] ne "text"E= not $tree[-3][1] =~ /\n$/uF= $tree[-2][0] ne "line"G= $tree[-1][0] ne "text"H= push @tree, ["line"]
ABCDEFGHresult
0X00000X0
1000000X0
11XXXXX00
0X1XXXX00
0X01XXX00
0X001XX00
101XXXX00
1001XXX00
10001XX00
0X0001X00
0X0000100
100001X00
100000100
11XXXXX11
0X1XXXX11
0X01XXX11
0X001XX11
101XXXX11
1001XXX11
10001XX11
0X0001X11
0X0000111
100001X11
100000111
131 if $tree[-4] && $tree[-4][0] ne 'line'
132 || (!$tree[-3] || $tree[-3][0] ne 'text' || $tree[-3][1] !~ /\n$/)
133 || ($tree[-2][0] ne 'line' || $tree[-1][0] ne 'text');
134 822 $tree[-3][1] .= pop(@tree)->[1];
135 }
136  
137 199 return $self;
138 }
139  
140 226 ▶sub process {
Subroutine process: called CC=4 SCAR=13.9
Pod documented
141 226 my $self = shift;
142  
143 # Use a local stack trace for compile exceptions
144 226 my $compiled = $self->compiled;
145 226 ▶ unless ($compiled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compiled) 199 27 226
146 199 my $code = $self->_compile->code;
147 199 monkey_patch $self->namespace, '_escape', $self->escape;
148 199 ▶ return Mojo::Exception->new($@)->inspect($self->unparsed, $code)->trace->verbose(1)
Branch: 1 branch
BranchTrueFalseTotal
unless $compiled = eval $self->_wrap($code, @_) 7 192 199
149 unless $compiled = eval $self->_wrap($code, @_);
150 192 $self->compiled($compiled);
151 }
152  
153 # Use a real stack trace for normal exceptions
154 13 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
155 13 ▶ CORE::die $_[0] if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 2 11 13
156 11 CORE::die Mojo::Exception->new(shift)->trace->inspect($self->unparsed, $self->code)->verbose(1);
157 219 };
158  
159 219 my $output;
160 219 ▶ return eval { $output = $compiled->(@_); 1 } ? $output : $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $output = &$compiled(@_); 1 } } ? : 211 8 219
161 }
162  
163 198 ▶sub render { shift->parse(shift)->process(@_) }
Subroutine render: called CC=1 SCAR=0.0
Pod documented
164  
165 56 ▶sub render_file {
Subroutine render_file: called CC=4 SCAR=13.9
Pod documented
166 56 my ($self, $path) = (shift, shift);
167  
168 56 ▶ $self->name($path) unless defined $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"name"} 4 52 56
169 56 my $template = path($path)->slurp;
170 56 my $encoding = $self->encoding;
171 56 ▶ croak qq{Template "$path" has invalid encoding} if $encoding && !defined($template = decode $encoding, $template);
Branch: 1 branch
BranchTrueFalseTotal
if $encoding and not defined($template = decode($encoding, $template)) 1 55 56
Condition66%2/3
$encoding and not defined($template = decode($encoding, $template))
!ll&&!rl&&r
0551
MC/DC50%1/2
$encoding and not defined($template = decode($encoding, $template))
$encoding not defined($template = decode($encoding, $template))
Truth table66%2/3
A and not(B)
A= $encodingB= not defined($template = decode($encoding, $template))
ABresult
0X0
100
111
172  
173 55 return $self->render($template, @_);
174 }
175  
176 199 ▶sub _compile {
Subroutine _compile: called CC=28 SCAR=33.3
177 199 my $self = shift;
178  
179 199 my $tree = $self->tree;
180 199 my $escape = $self->auto_escape;
181  
182 199 my @blocks = ('');
183 199 my ($i, $capture, $multi);
184 199 ▶ while (++$i <= @$tree && (my $next = $tree->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i <= @$tree and my $next = $tree->[$i]) 5422 199 5621
Condition100%3/3
++$i <= @$tree and my $next = $tree->[$i]
!ll&&!rl&&r
11985422
MC/DC100%2/2
++$i <= @$tree and my $next = $tree->[$i]
++$i <= @$tree my $next = $tree->[$i]
Truth table100%3/3
A and B
A= ++$i <= @$treeB= my $next = $tree->[$i]
ABresult
0X0
100
111
185 5422 my ($op, $value) = @{$tree->[$i - 1]};
186 5422 ▶ push @blocks, '' and next if $op eq 'line';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "line" 978 4444 5422
Condition60%3/5
$op eq 'line' && (push(@blocks, '') && (next))
!ll&&!rl&&r
44449780
push @blocks, "" and next
!ll
0978
MC/DC0%0/2
$op eq 'line' && (push(@blocks, '') && (next))
$op eq 'line' push @blocks, ""
Truth table33%1/3
A && B
A= $op eq 'line'B= push @blocks, ""
ABresult
0X0
100
111
187 4444 ▶ my $newline = chomp($value //= '');
Condition100%2/2
$value //= ""
l!l
4338106
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
188  
189 # Text (quote and fix line ending)
190 4444 ▶ if ($op eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'text') { } 2984 1460 4444
elsif ($op eq 'code' or $multi) { } 917 543 1460
elsif ($op eq 'cpen') { } 53 490 543
191 2984 $value = join "\n", map { quotemeta $_ } split(/\n/, $value, -1);
192 2984 ▶ $value .= '\n' if $newline;
Branch: 1 branch
BranchTrueFalseTotal
if $newline 711 2273 2984
193 2984 ▶ $blocks[-1] .= "\$_O .= \"" . $value . "\";" if length $value;
Branch: 1 branch
BranchTrueFalseTotal
if length $value 1058 1926 2984
194 }
195  
196 # Code or multi-line expression
197 917 ▶ elsif ($op eq 'code' || $multi) { $blocks[-1] .= $value }
Condition100%3/3
$op eq "code" or $multi
l!l&&r!l&&!r
90314543
MC/DC100%2/2
$op eq "code" or $multi
$op eq "code" $multi
Truth table100%3/3
A or B
A= $op eq "code"B= $multi
ABresult
1X1
011
000
198  
199 # Capture end
200 elsif ($op eq 'cpen') {
201 53 $blocks[-1] .= 'return Mojo::ByteStream->new($_O) }';
202  
203 # No following code
204 53 ▶ $blocks[-1] .= ';' if $next->[0] ne 'cpst' && ($next->[1] // '') =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[0] ne "cpst" and ($next->[1] // "") =~ /^\s*$/u 47 6 53
Condition80%4/5
$next->[0] ne "cpst" and ($next->[1] // "") =~ /^\s*$/u
!ll&&!rl&&r
1547
$next->[1] // ""
l!l
520
MC/DC50%1/2
$next->[0] ne "cpst" and ($next->[1] // "") =~ /^\s*$/u
$next->[0] ne "cpst" $next->[1]
Truth table66%2/3
A and B
A= $next->[0] ne "cpst"B= $next->[1]
ABresult
0X0
100
111
205 }
206  
207 # Expression
208 4444 ▶ if ($op eq 'expr' || $op eq 'escp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "expr" or $op eq "escp") 451 3993 4444
Condition100%3/3
$op eq "expr" or $op eq "escp"
l!l&&r!l&&!r
428233993
MC/DC100%2/2
$op eq "expr" or $op eq "escp"
$op eq "expr" $op eq "escp"
Truth table100%3/3
A or B
A= $op eq "expr"B= $op eq "escp"
ABresult
1X1
011
000
209  
210 # Escaped
211 451 ▶ if (!$multi && ($op eq 'escp' && !$escape || $op eq 'expr' && $escape)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $multi and $op eq 'escp' && !$escape || $op eq 'expr' && $escape) { } 251 200 451
elsif (not $multi) { } 186 14 200
Condition100%12/12
not $multi and $op eq "escp" && !$escape || $op eq "expr" && $escape
!ll&&!rl&&r
14186251
$op eq "escp" and not $escape or $op eq "expr" and $escape
l!l&&r!l&&!r
19232186
$op eq "escp" and not $escape
!ll&&!rl&&r
417119
$op eq "expr" and $escape
!ll&&!rl&&r
1185232
MC/DC80%4/5
not $multi and $op eq "escp" && !$escape || $op eq "expr" && $escape
not $multi $op eq "escp" not $escape $op eq "expr" $escape
Truth table62%5/8
not(A) and B and not(C) or D and E
A= not $multiB= $op eq "escp"C= not $escapeD= $op eq "expr"E= $escape
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
212 251 $blocks[-1] .= "\$_O .= _escape scalar + $value";
213 }
214  
215 # Raw
216 186 elsif (!$multi) { $blocks[-1] .= "\$_O .= scalar + $value" }
217  
218 # Multi-line
219 451 ▶ $multi = !$next || $next->[0] ne 'text';
Condition66%2/3
!$next || $next->[0] ne 'text'
l!l&&r!l&&!r
014437
MC/DC50%1/2
!$next || $next->[0] ne 'text'
!$next $next->[0] ne 'text'
Truth table66%2/3
not(A) || B
A= !$nextB= $next->[0] ne 'text'
ABresult
1X1
011
000
220  
221 # Append semicolon
222 451 ▶ $blocks[-1] .= ';' unless $multi || $capture;
Branch: 1 branch
BranchTrueFalseTotal
unless $multi or $capture 422 29 451
Condition100%3/3
$multi or $capture
l!l&&r!l&&!r
1415422
MC/DC100%2/2
$multi or $capture
$multi $capture
Truth table100%3/3
A or B
A= $multiB= $capture
ABresult
1X1
011
000
223 }
224  
225 # Capture start
226 4444 ▶ if ($op eq 'cpst') { $capture = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'cpst') { } 53 4391 4444
elsif ($capture) { } 53 4338 4391
227 elsif ($capture) {
228 53 $blocks[-1] .= "sub { my \$_O = ''; ";
229 53 $capture = 0;
230 }
231 }
232  
233 199 return $self->code(join "\n", @blocks)->tree([]);
234 }
235  
236 398 ▶sub _line {
Subroutine _line: called CC=1 SCAR=0.0
237 398 my $name = shift->name;
238 398 $name =~ y/"//d;
239 398 return qq{#line @{[shift]} "$name"};
240 }
241  
242 426 ▶sub _trim {
Subroutine _trim: called CC=5 SCAR=16.1
243 426 my $tree = shift;
244  
245 # Skip captures
246 426 ▶ my $i = $tree->[-2][0] eq 'cpst' || $tree->[-2][0] eq 'cpen' ? -3 : -2;
Branch: 1 branch
BranchTrueFalseTotal
$tree->[-2][0] eq 'cpst' || $tree->[-2][0] eq 'cpen' ? : 73 353 426
Condition100%3/3
$tree->[-2][0] eq "cpst" or $tree->[-2][0] eq "cpen"
l!l&&r!l&&!r
3439353
MC/DC100%2/2
$tree->[-2][0] eq "cpst" or $tree->[-2][0] eq "cpen"
$tree->[-2][0] eq "cpst" $tree->[-2][0] eq "cpen"
Truth table100%3/3
A or B
A= $tree->[-2][0] eq "cpst"B= $tree->[-2][0] eq "cpen"
ABresult
1X1
011
000
247  
248 # Only trim text
249 426 ▶ return unless $tree->[$i][0] eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->[$i][0] eq "text" 9 417 426
250  
251 # Convert whitespace text to line noise
252 417 ▶ splice @$tree, $i, 0, ['code', $1] if $tree->[$i][1] =~ s/(\s+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->[$i][1] =~ s/(\s+)$//u 17 400 417
253 }
254  
255 199 ▶sub _wrap {
Subroutine _wrap: called CC=3 SCAR=11.0
256 199 my ($self, $body, $vars) = @_;
257  
258 # Variables
259 199 my $args = '';
260 199 ▶ if ($self->vars && (my @vars = grep {/^\w+$/} keys %$vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->vars and my(@vars) = grep({/^\w+$/u;} keys %$vars)) 32 167 199
Condition100%3/3
$self->vars and my(@vars) = grep({/^\w+$/u;} keys %$vars)
!ll&&!rl&&r
1551232
MC/DC100%2/2
$self->vars and my(@vars) = grep({/^\w+$/u;} keys %$vars)
$self->vars my(@vars) = grep({/^\w+$/u;} keys %$vars)
Truth table100%3/3
A and B
A= $self->varsB= my(@vars) = grep({/^\w+$/u;} keys %$vars)
ABresult
0X0
100
111
261 32 $args = 'my (' . join(',', map {"\$$_"} @vars) . ')';
262 32 $args .= '= @{shift()}{qw(' . join(' ', @vars) . ')};';
263 }
264  
265 # Wrap lines
266 199 my $num = () = $body =~ /\n/g;
267 199 my $code = $self->_line(1) . "\npackage @{[$self->namespace]};";
268 199 $code .= "use Mojo::Base -strict; no warnings 'ambiguous';";
269 199 $code .= "sub { my \$_O = ''; @{[$self->prepend]};{ $args { $body\n";
270 199 $code .= $self->_line($num + 1) . "\n;}@{[$self->append]}; } \$_O };";
271  
272 199 warn "-- Code for @{[$self->name]}\n@{[encode 'UTF-8', $code]}\n\n" if DEBUG;
273 199 return $code;
274 }
275  
276 21 1;
277  
278 =encoding utf8
279  
280 =head1 NAME
281  
282 Mojo::Template - Perl-ish templates
283  
284 =head1 SYNOPSIS
285  
286 use Mojo::Template;
287  
288 # Use Perl modules
289 my $mt = Mojo::Template->new;
290 say $mt->render(<<'EOF');
291 % use Time::Piece;
292 <div>
293 % my $now = localtime;
294 Time: <%= $now->hms %>
295 </div>
296 EOF
297  
298 # Render with arguments
299 say $mt->render(<<'EOF', [1 .. 13], 'Hello World!');
300 % my ($numbers, $title) = @_;
301 <div>
302 <h1><%= $title %></h1>
303 % for my $i (@$numbers) {
304 Test <%= $i %>
305 % }
306 </div>
307 EOF
308  
309 # Render with named variables
310 say $mt->vars(1)->render(<<'EOF', {title => 'Hello World!'});
311 <div>
312 <h1><%= $title %></h1>
313 %= 5 + 5
314 </div>
315 EOF
316  
317 =head1 DESCRIPTION
318  
319 L<Mojo::Template> is a minimalistic, fast, and very Perl-ish template engine, designed specifically for all those small
320 tasks that come up during big projects. Like preprocessing a configuration file, generating text from heredocs and
321 stuff like that.
322  
323 See L<Mojolicious::Guides::Rendering> for information on how to generate content with the L<Mojolicious> renderer.
324  
325 =head1 SYNTAX
326  
327 For all templates L<strict>, L<warnings>, L<utf8> and Perl 5.16 L<features|feature> are automatically enabled.
328  
329 <% Perl code %>
330 <%= Perl expression, replaced with result %>
331 <%== Perl expression, replaced with XML escaped result %>
332 <%# Comment, useful for debugging %>
333 <%% Replaced with "<%", useful for generating templates %>
334 % Perl code line, treated as "<% line =%>" (explained later)
335 %= Perl expression line, treated as "<%= line %>"
336 %== Perl expression line, treated as "<%== line %>"
337 %# Comment line, useful for debugging
338 %% Replaced with "%", useful for generating templates
339  
340 Escaping behavior can be reversed with the L</"auto_escape"> attribute, this is the default in L<Mojolicious> C<.ep>
341 templates, for example.
342  
343 <%= Perl expression, replaced with XML escaped result %>
344 <%== Perl expression, replaced with result %>
345  
346 L<Mojo::ByteStream> objects are always excluded from automatic escaping.
347  
348 % use Mojo::ByteStream qw(b);
349 <%= b('<div>excluded!</div>') %>
350  
351 Whitespace characters around tags can be trimmed by adding an additional equal sign to the end of a tag.
352  
353 <% for (1 .. 3) { %>
354 <%= 'Trim all whitespace characters around this expression' =%>
355 <% } %>
356  
357 Newline characters can be escaped with a backslash.
358  
359 This is <%= 1 + 1 %> a\
360 single line
361  
362 And a backslash in front of a newline character can be escaped with another backslash.
363  
364 This will <%= 1 + 1 %> result\\
365 in multiple\\
366 lines
367  
368 A newline character gets appended automatically to every template, unless the last character is a backslash. And empty
369 lines at the end of a template are ignored.
370  
371 There is <%= 1 + 1 %> no newline at the end here\
372  
373 You can capture whole template blocks for reuse later with the C<begin> and C<end> keywords. Just be aware that both
374 keywords are part of the surrounding tag and not actual Perl code, so there can only be whitespace after C<begin> and
375 before C<end>.
376  
377 <% my $block = begin %>
378 <% my $name = shift; =%>
379 Hello <%= $name %>.
380 <% end %>
381 <%= $block->('Baerbel') %>
382 <%= $block->('Wolfgang') %>
383  
384 Perl lines can also be indented freely.
385  
386 % my $block = begin
387 % my $name = shift;
388 Hello <%= $name %>.
389 % end
390 %= $block->('Baerbel')
391 %= $block->('Wolfgang')
392  
393 L<Mojo::Template> templates get compiled to a Perl subroutine, that means you can access arguments simply via C<@_>.
394  
395 % my ($foo, $bar) = @_;
396 % my $x = shift;
397 test 123 <%= $foo %>
398  
399 The compilation of templates to Perl code can make debugging a bit tricky, but L<Mojo::Template> will return
400 L<Mojo::Exception> objects that stringify to error messages with context.
401  
402 Bareword "xx" not allowed while "strict subs" in use at template line 4.
403 Context:
404 2: </head>
405 3: <body>
406 4: % my $i = 2; xx
407 5: %= $i * 2
408 6: </body>
409 Traceback (most recent call first):
410 File "template", line 4, in "Mojo::Template::Sandbox"
411 File "path/to/Mojo/Template.pm", line 123, in "Mojo::Template"
412 File "path/to/myapp.pl", line 123, in "main"
413  
414 =head1 ATTRIBUTES
415  
416 L<Mojo::Template> implements the following attributes.
417  
418 =head2 auto_escape
419  
420 my $bool = $mt->auto_escape;
421 $mt = $mt->auto_escape($bool);
422  
423 Activate automatic escaping.
424  
425 # "&lt;html&gt;"
426 Mojo::Template->new(auto_escape => 1)->render("<%= '<html>' %>");
427  
428 =head2 append
429  
430 my $code = $mt->append;
431 $mt = $mt->append('warn "Processed template"');
432  
433 Append Perl code to compiled template. Note that this code should not contain newline characters, or line numbers in
434 error messages might end up being wrong.
435  
436 =head2 capture_end
437  
438 my $end = $mt->capture_end;
439 $mt = $mt->capture_end('end');
440  
441 Keyword indicating the end of a capture block, defaults to C<end>.
442  
443 <% my $block = begin %>
444 Some data!
445 <% end %>
446  
447 =head2 capture_start
448  
449 my $start = $mt->capture_start;
450 $mt = $mt->capture_start('begin');
451  
452 Keyword indicating the start of a capture block, defaults to C<begin>.
453  
454 <% my $block = begin %>
455 Some data!
456 <% end %>
457  
458 =head2 code
459  
460 my $code = $mt->code;
461 $mt = $mt->code($code);
462  
463 Perl code for template if available.
464  
465 =head2 comment_mark
466  
467 my $mark = $mt->comment_mark;
468 $mt = $mt->comment_mark('#');
469  
470 Character indicating the start of a comment, defaults to C<#>.
471  
472 <%# This is a comment %>
473  
474 =head2 compiled
475  
476 my $compiled = $mt->compiled;
477 $mt = $mt->compiled($compiled);
478  
479 Compiled template code if available.
480  
481 =head2 encoding
482  
483 my $encoding = $mt->encoding;
484 $mt = $mt->encoding('UTF-8');
485  
486 Encoding used for template files, defaults to C<UTF-8>.
487  
488 =head2 escape
489  
490 my $cb = $mt->escape;
491 $mt = $mt->escape(sub {...});
492  
493 A callback used to escape the results of escaped expressions, defaults to L<Mojo::Util/"xml_escape">.
494  
495 $mt->escape(sub ($str) { return reverse $str });
496  
497 =head2 escape_mark
498  
499 my $mark = $mt->escape_mark;
500 $mt = $mt->escape_mark('=');
501  
502 Character indicating the start of an escaped expression, defaults to C<=>.
503  
504 <%== $foo %>
505  
506 =head2 expression_mark
507  
508 my $mark = $mt->expression_mark;
509 $mt = $mt->expression_mark('=');
510  
511 Character indicating the start of an expression, defaults to C<=>.
512  
513 <%= $foo %>
514  
515 =head2 line_start
516  
517 my $start = $mt->line_start;
518 $mt = $mt->line_start('%');
519  
520 Character indicating the start of a code line, defaults to C<%>.
521  
522 % $foo = 23;
523  
524 =head2 name
525  
526 my $name = $mt->name;
527 $mt = $mt->name('foo.mt');
528  
529 Name of template currently being processed, defaults to C<template>. Note that this value should not contain quotes or
530 newline characters, or error messages might end up being wrong.
531  
532 =head2 namespace
533  
534 my $namespace = $mt->namespace;
535 $mt = $mt->namespace('main');
536  
537 Namespace used to compile templates, defaults to C<Mojo::Template::Sandbox>. Note that namespaces should only be shared
538 very carefully between templates, since functions and global variables will not be cleared automatically.
539  
540 =head2 prepend
541  
542 my $code = $mt->prepend;
543 $mt = $mt->prepend('my $self = shift;');
544  
545 Prepend Perl code to compiled template. Note that this code should not contain newline characters, or line numbers in
546 error messages might end up being wrong.
547  
548 =head2 replace_mark
549  
550 my $mark = $mt->replace_mark;
551 $mt = $mt->replace_mark('%');
552  
553 Character used for escaping the start of a tag or line, defaults to C<%>.
554  
555 <%% my $foo = 23; %>
556  
557 =head2 tag_start
558  
559 my $start = $mt->tag_start;
560 $mt = $mt->tag_start('<%');
561  
562 Characters indicating the start of a tag, defaults to C<E<lt>%>.
563  
564 <% $foo = 23; %>
565  
566 =head2 tag_end
567  
568 my $end = $mt->tag_end;
569 $mt = $mt->tag_end('%>');
570  
571 Characters indicating the end of a tag, defaults to C<%E<gt>>.
572  
573 <%= $foo %>
574  
575 =head2 tree
576  
577 my $tree = $mt->tree;
578 $mt = $mt->tree([['text', 'foo'], ['line']]);
579  
580 Template in parsed form if available. Note that this structure should only be used very carefully since it is very
581 dynamic.
582  
583 =head2 trim_mark
584  
585 my $mark = $mt->trim_mark;
586 $mt = $mt->trim_mark('-');
587  
588 Character activating automatic whitespace trimming, defaults to C<=>.
589  
590 <%= $foo =%>
591  
592 =head2 unparsed
593  
594 my $unparsed = $mt->unparsed;
595 $mt = $mt->unparsed('<%= 1 + 1 %>');
596  
597 Raw unparsed template if available.
598  
599 =head2 vars
600  
601 my $bool = $mt->vars;
602 $mt = $mt->vars($bool);
603  
604 Instead of a list of values, use a hash reference with named variables to pass data to templates.
605  
606 # "works!"
607 Mojo::Template->new(vars => 1)->render('<%= $test %>!', {test => 'works'});
608  
609 =head1 METHODS
610  
611 L<Mojo::Template> inherits all methods from L<Mojo::Base> and implements the following new ones.
612  
613 =head2 parse
614  
615 $mt = $mt->parse('<%= 1 + 1 %>');
616  
617 Parse template into L</"tree">.
618  
619 =head2 process
620  
621 my $output = $mt->process;
622 my $output = $mt->process(@args);
623 my $output = $mt->process({foo => 'bar'});
624  
625 Process previously parsed template and return the result, or a L<Mojo::Exception> object if rendering failed.
626  
627 # Parse and process
628 say Mojo::Template->new->parse('Hello <%= $_[0] %>')->process('Bender');
629  
630 # Reuse template (for much better performance)
631 my $mt = Mojo::Template->new;
632 say $mt->render('Hello <%= $_[0] %>!', 'Bender');
633 say $mt->process('Fry');
634 say $mt->process('Leela');
635  
636 =head2 render
637  
638 my $output = $mt->render('<%= 1 + 1 %>');
639 my $output = $mt->render('<%= shift() + shift() %>', @args);
640 my $output = $mt->render('<%= $foo %>', {foo => 'bar'});
641  
642 Render template and return the result, or a L<Mojo::Exception> object if rendering failed.
643  
644 # Longer version
645 my $output = $mt->parse('<%= 1 + 1 %>')->process;
646  
647 # Render with arguments
648 say Mojo::Template->new->render('<%= $_[0] %>', 'bar');
649  
650 # Render with named variables
651 say Mojo::Template->new(vars => 1)->render('<%= $foo %>', {foo => 'bar'});
652  
653 =head2 render_file
654  
655 my $output = $mt->render_file('/tmp/foo.mt');
656 my $output = $mt->render_file('/tmp/foo.mt', @args);
657 my $output = $mt->render_file('/tmp/bar.mt', {foo => 'bar'});
658  
659 Same as L</"render">, but renders a template file.
660  
661 =head1 DEBUGGING
662  
663 You can set the C<MOJO_TEMPLATE_DEBUG> environment variable to get some advanced diagnostics information printed to
664 C<STDERR>.
665  
666 MOJO_TEMPLATE_DEBUG=1
667  
668 =head1 SEE ALSO
669  
670 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
671  
672 =cut
« Mojolicious/Command/Author/inflate.pm ↑ Summary Mojolicious/Routes/Match.pm »