blib/lib/Venus/Template.pm

filter: Statementstmt 83.2% 109 / 131 Branchbran 71.8% 23 / 32 Conditioncond 62.0% 18 / 29 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 73.6% 14 / 19 Podpod 10.0% 1 / 10 totaltotal 72.4% 171 / 236 CC 30 SCAR 18.5
CC
30
Cov
78%
render_if_not
20.0
mappable
12.1
render_blocks
5.1
CRAP 6.3
« blib/lib/Venus/Error.pm ↑ Summary blib/lib/Venus/Role/Catchable.pm »
1 package Venus::Template;
2  
3 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 3 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 3 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 3 with 'Venus::Role::Valuable';
19 3 with 'Venus::Role::Buildable';
20 3 with 'Venus::Role::Accessible';
21 3 with 'Venus::Role::Explainable';
22  
23 # STATE
24  
25 3 state $TOKEN_PATH_NOTATION = qr/[a-z_][\w.]*/;
26 3 state $TOKEN_TAG_OPEN = qr/\{\{/;
27 3 state $TOKEN_TAG_CLOSE = qr/\}\}/;
28  
29 # OVERLOADS
30  
31 3 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=1.7
32 '""' => 'explain',
33 0 ▶ 'eq' => sub{$_[0]->render eq "$_[1]"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ 'ne' => sub{$_[0]->render ne "$_[1]"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 ▶ 'qr' => sub{qr/@{[quotemeta($_[0]->render)]}/},
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 3 '~~' => 'explain',
37 fallback => 1,
38 3 );
39  
40 # ATTRIBUTES
41  
42 3 attr 'context';
43  
44 # BUILDERS
45  
46 25 ▶sub build_data {
Subroutine build_data: called CC=3 SCAR=11.0
Pod undocumented
47 25 my ($self, $data, $args) = @_;
48  
49 25 ▶ $data->{context} = $data->{context} ? ({%{$data->{context}}, %{$args}}) : $args;
Branch: 1 branch
BranchTrueFalseTotal
$data->{'context'} ? : 1 24 25
50  
51 25 ▶ $data->{value} ||= '';
Condition100%2/2
$data->{"value"} ||= ""
l!l
232
MC/DC100%1/1
$data->{"value"} ||= ""
$data->{"value"}
Truth table100%2/2
A
A= $data->{"value"}
Aresult
11
00
52  
53 25 return $data;
54 }
55  
56 # METHODS
57  
58 0 ▶sub default {
Subroutine default: not called CC=1 SCAR=6.9
Pod undocumented
59  
60 0 return '';
61 }
62  
63 11 ▶sub explain {
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
64 11 my ($self) = @_;
65  
66 11 return $self->render;
67 }
68  
69 278 ▶sub mappable {
Subroutine mappable: called CC=10 SCAR=25.0
Pod undocumented
70 278 my ($self, $data) = @_;
71  
72 278 require Scalar::Util;
73 278 require Venus::Array;
74 278 require Venus::Hash;
75  
76 278 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 278 278
77 0 return Venus::Hash->new;
78 }
79 278 ▶ if (!Scalar::Util::blessed($data) && ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) and ref $data eq "ARRAY") 12 266 278
Condition100%3/3
not builtin::blessed($data) and ref $data eq "ARRAY"
!ll&&!rl&&r
6819812
MC/DC100%2/2
not builtin::blessed($data) and ref $data eq "ARRAY"
not builtin::blessed($data) ref $data eq "ARRAY"
Truth table100%3/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "ARRAY"
ABresult
0X0
100
111
80 12 return Venus::Array->new($data);
81 }
82 266 ▶ if (!Scalar::Util::blessed($data) && ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) and ref $data eq "HASH") 198 68 266
Condition66%2/3
not builtin::blessed($data) and ref $data eq "HASH"
!ll&&!rl&&r
680198
MC/DC50%1/2
not builtin::blessed($data) and ref $data eq "HASH"
not builtin::blessed($data) ref $data eq "HASH"
Truth table66%2/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "HASH"
ABresult
0X0
100
111
83 198 return Venus::Hash->new($data);
84 }
85 68 ▶ if (!Scalar::Util::blessed($data) || (Scalar::Util::blessed($data)
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) or builtin::blessed($data) and not $data->isa('Venus::Array') || $data->isa('Venus::Hash')) { } 0 68 68
Condition44%4/9
not builtin::blessed($data) or builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
l!l&&r!l&&!r
0068
builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
!ll&&!rl&&r
0680
$data->isa('Venus::Array') || $data->isa('Venus::Hash')
l!l&&r!l&&!r
24440
MC/DC0%0/4
not builtin::blessed($data) or builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
not builtin::blessed($data) builtin::blessed($data) $data->isa('Venus::Array') $data->isa('Venus::Hash')
Truth table40%2/5
not(A) or B and not(C || D)
A= not builtin::blessed($data)B= builtin::blessed($data)C= $data->isa('Venus::Array')D= $data->isa('Venus::Hash')
ABCDresult
1XXX1
01001
00XX0
011X0
01010
86 && !($data->isa('Venus::Array') || $data->isa('Venus::Hash'))))
87 {
88 0 return Venus::Hash->new;
89 }
90 else {
91 68 return $data;
92 }
93 }
94  
95 63 ▶sub render {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
96 63 my ($self, $content, $context, $parent_loops) = @_;
97  
98 63 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 27 36 63
99 27 $content = $self->get;
100 }
101  
102 63 ▶ if (!defined $context) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $context) { } 24 39 63
103 24 $context = $self->context;
104 }
105 else {
106 39 $context = $self->mappable($self->context)->merge(
107 $self->mappable($context)->get
108 );
109 }
110  
111 63 $content =~ s/^\r?\n//;
112 63 $content =~ s/\r?\n\ *$//;
113  
114 63 ▶ $parent_loops ||= [];
Condition100%2/2
$parent_loops ||= []
l!l
3627
MC/DC100%1/1
$parent_loops ||= []
$parent_loops
Truth table100%2/2
A
A= $parent_loops
Aresult
11
00
115  
116 63 $content = $self->render_blocks($content, $context, $parent_loops);
117  
118 63 $content = $self->render_tokens($content, $context);
119  
120 63 return $content;
121 }
122  
123 63 ▶sub render_blocks {
Subroutine render_blocks: called CC=5 SCAR=16.4
Pod undocumented
124 63 my ($self, $content, $context, $parent_loops) = @_;
125  
126 63 my $token_tag_open = $TOKEN_TAG_OPEN;
127  
128 63 my $token_tag_close = $TOKEN_TAG_CLOSE;
129  
130 63 my $path = $TOKEN_PATH_NOTATION;
131  
132 63 my $regexp = qr{
133 (?<!\\)
134 (?<!\\\{)
135 $token_tag_open
136 \s*
137 (FOR|IF|IF\sNOT)
138 \s+
139 ($path)
140 \s*
141 $token_tag_close
142 (?!\})
143 (.+?)
144 (?<!\\)
145 (?<!\\\{)
146 $token_tag_open
147 \s*
148 (END)
149 \s+
150 \2
151 \s*
152 $token_tag_close
153 (?!\})
154 }xis;
155  
156 63 ▶ $parent_loops ||= [];
Condition50%1/2
$parent_loops ||= []
l!l
630
MC/DC0%0/1
$parent_loops ||= []
$parent_loops
Truth table50%1/2
A
A= $parent_loops
Aresult
11
00
157  
158 63 $context = $self->mappable($context);
159  
160 63 $content =~ s{
161 $regexp
162 }{
163 20 my ($type, $path, $block) = ($1, $2, $3);
164 20 ▶ if (lc($type) eq 'if') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $type eq 'if') { } 8 12 20
elsif (lc $type eq 'if not') { } 0 12 12
elsif (lc $type eq 'for') { } 12 0 12
165 8 $self->render_if(
166 $block, $context, !!scalar($context->path($path)), $path, $parent_loops,
167 );
168 }
169 elsif (lc($type) eq 'if not') {
170 0 $self->render_if_not(
171 $block, $context, !!scalar($context->path($path)), $path, $parent_loops,
172 );
173 }
174 elsif (lc($type) eq 'for') {
175 12 $self->render_foreach(
176 $block, $self->mappable($context->path($path)), $parent_loops,
177 );
178 }
179 }gsex;
180  
181 63 $content =~ s/\\(\{\{|\}\})/$1/g;
182  
183 63 return $content;
184 }
185  
186 8 ▶sub render_if {
Subroutine render_if: called CC=4 SCAR=13.9
Pod undocumented
187 8 my ($self, $content, $context, $boolean, $path, $parent_loops) = @_;
188  
189 8 my $mappable = $self->mappable($context);
190  
191 8 my $token_tag_open = $TOKEN_TAG_OPEN;
192  
193 8 my $token_tag_close = $TOKEN_TAG_CLOSE;
194  
195 8 $path = quotemeta $path;
196  
197 8 my $regexp = qr{
198 $token_tag_open
199 \s*
200 ELSE
201 \s+
202 $path
203 \s*
204 $token_tag_close
205 }xis;
206  
207 8 ▶ $parent_loops ||= [];
Condition50%1/2
$parent_loops ||= []
l!l
80
MC/DC0%0/1
$parent_loops ||= []
$parent_loops
Truth table50%1/2
A
A= $parent_loops
Aresult
11
00
208  
209 8 my ($a, $b) = split /$regexp/, $content;
210  
211 8 ▶ if ($boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean) { } 5 3 8
212 5 return $self->render($a, $mappable, $parent_loops);
213 }
214 else {
215 3 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 1 2 3
216 1 return $self->render($b, $mappable, $parent_loops);
217 }
218 else {
219 2 return '';
220 }
221 }
222 }
223  
224 0 ▶sub render_if_not {
Subroutine render_if_not: not called CC=4 SCAR=30.0
Pod undocumented
225 0 my ($self, $content, $context, $boolean, $path, $parent_loops) = @_;
226  
227 0 my $mappable = $self->mappable($context);
228  
229 0 my $token_tag_open = $TOKEN_TAG_OPEN;
230  
231 0 my $token_tag_close = $TOKEN_TAG_CLOSE;
232  
233 0 $path = quotemeta $path;
234  
235 0 my $regexp = qr{
236 $token_tag_open
237 \s*
238 ELSE
239 \s+
240 $path
241 \s*
242 $token_tag_close
243 }xis;
244  
245 0 ▶ $parent_loops ||= [];
Condition0%0/2
$parent_loops ||= []
l!l
00
MC/DC0%0/1
$parent_loops ||= []
$parent_loops
Truth table0%0/2
A
A= $parent_loops
Aresult
11
00
246  
247 0 my ($a, $b) = split /$regexp/, $content;
248  
249 0 ▶ if (!$boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $boolean) { } 0 0 0
250 0 return $self->render($a, $mappable, $parent_loops);
251 }
252 else {
253 0 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 0 0 0
254 0 return $self->render($b, $mappable, $parent_loops);
255 }
256 else {
257 0 return '';
258 }
259 }
260 }
261  
262 12 ▶sub render_foreach {
Subroutine render_foreach: called CC=3 SCAR=11.0
Pod undocumented
263 12 my ($self, $content, $context, $parent_loops) = @_;
264  
265 12 $context = $self->mappable($context);
266  
267 12 ▶ if (!$context->isa('Venus::Array')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->isa("Venus::Array")) 0 12 12
268 0 return '';
269 }
270  
271 12 ▶ $parent_loops ||= [];
Condition50%1/2
$parent_loops ||= []
l!l
120
MC/DC0%0/1
$parent_loops ||= []
$parent_loops
Truth table50%1/2
A
A= $parent_loops
Aresult
11
00
272  
273 30 ▶ my @results = $self->mappable($context)->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
274 30 my (@args) = @_;
275  
276 30 my $value = $args[1];
277  
278 30 my $loop = {
279 index => $args[0],
280 item => $value,
281 level => {},
282 parent => $parent_loops->[0],
283 place => $args[0]+1,
284 };
285  
286 30 my $iteration_parent_loops = [$loop, @{$parent_loops}];
287  
288 $loop->{level}->{$_} = $iteration_parent_loops->[$_]
289 30 for map {$#{$iteration_parent_loops} - $_} 0..$#{$iteration_parent_loops};
290  
291 30 ▶ $value = ref($value) ? $value : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 15 15 30
292  
293 30 $self->render($content, $self->mappable($value)->do('set', 'loop', $loop), $iteration_parent_loops);
294 12 });
295  
296 12 return join "\n", grep !!$_, @results;
297 }
298  
299 63 ▶sub render_tokens {
Subroutine render_tokens: called CC=2 SCAR=6.9
Pod undocumented
300 63 my ($self, $content, $context) = @_;
301  
302 63 my $token_tag_open = $TOKEN_TAG_OPEN;
303  
304 63 my $token_tag_close = $TOKEN_TAG_CLOSE;
305  
306 63 my $path = $TOKEN_PATH_NOTATION;
307  
308 63 my $regexp = qr{
309 (?<!\\)
310 (?<!\\\{)
311 $token_tag_open
312 \s*
313 ($path)
314 \s*
315 $token_tag_close
316 (?!\})
317 }xi;
318  
319 63 $context = $self->mappable($context);
320  
321 63 $content =~ s{
322 $regexp
323 }{
324 88 ▶ scalar($context->path($1)) // ''
Condition100%2/2
scalar $context->path($1) // ""
l!l
808
MC/DC100%1/1
scalar $context->path($1) // ""
scalar $context->path($1)
Truth table100%2/2
A
A= scalar $context->path($1)
Aresult
11
00
325 }gsex;
326  
327 63 $content =~ s/\\(\{\{|\}\})/$1/g;
328  
329 63 return $content;
330 }
331  
332 3 1;
333  
334  
335  
336 =head1 NAME
337  
338 Venus::Template - Template Class
339  
340 =cut
341  
342 =head1 ABSTRACT
343  
344 Template Class for Perl 5
345  
346 =cut
347  
348 =head1 SYNOPSIS
349  
350 package main;
351  
352 use Venus::Template;
353  
354 my $template = Venus::Template->new(
355 'From: <{{ email }}>',
356 );
357  
358 # $template->render;
359  
360 # "From: <>"
361  
362 =cut
363  
364 =head1 DESCRIPTION
365  
366 This package provides a simple yet powerful templating system for Perl 5 that
367 supports variable interpolation, loops, and conditional blocks. The templating
368 system is designed to only operate on raw Perl data and as such does not
369 support operators, filters, virtual method, etc.
370  
371 =head2 Syntax
372  
373 =head3 Tags
374  
375 The opening and closing tags are C<{{> and C<}}>. Content between these tags
376 can be:
377  
378 =over 4
379  
380 =item *
381  
382 Simple path access (e.g., C<{{ name }}>)
383  
384 =item *
385  
386 Nested path access (e.g., C<{{ user.email }}>)
387  
388 =item *
389  
390 Control structures (described below)
391  
392 =back
393  
394 To include literal tags in your template, escape them using either:
395  
396 =over 4
397  
398 =item *
399  
400 Single backslash: C<\{{> and C<\}}>
401  
402 =item *
403  
404 Escaped braces: C<\{\{> and C<\}\}>
405  
406 =back
407  
408 =head3 Tokens
409  
410 Variable substitution uses dot notation to access nested data:
411  
412 # access hash key 'name' in hash 'user'
413 {{ user.name }}
414  
415 # access hash key 'name' in first element of array 'users'
416 {{ users.0.name }}
417  
418 # access deeply nested hash keys
419 {{ profile.address.zip }}
420  
421 =head3 Loops
422  
423 Loops iterate over arrays using the C<for> control structure:
424  
425 {{ for users }}
426 Name: {{ user.name }}
427 {{ end users }}
428  
429 Special loop variables are available within C<for> blocks:
430  
431 =over 4
432  
433 =item *
434  
435 C<{{ loop.index }}> - Zero-based iteration counter
436  
437 =item *
438  
439 C<{{ loop.place }}> - One-based iteration counter
440  
441 =item *
442  
443 C<{{ loop.item }}> - Current item value (useful for arrays of simple values)
444  
445 =back
446  
447 For nested loops, parent loop information is accessible via:
448  
449  
450 =over 4
451  
452 =item *
453  
454 C<{{ loop.parent }}> - Immediate parent loop's data
455  
456 =item *
457  
458 C<{{ loop.level.0 }}> - First loop
459  
460 =item *
461  
462 C<{{ loop.level.1 }}> - Second loop
463  
464 =item *
465  
466 C<{{ loop.level.2 }}> - Third loop
467  
468 =back
469  
470 =head3 Controls
471  
472 Conditional blocks use C<if>, C<if not>, and optional C<else>:
473  
474 {{ if user.admin }}
475 Admin: {{ user.name }}
476 {{ else user.admin }}
477 User: {{ user.name }}
478 {{ end user.admin }}
479  
480 {{ if not user.active }}
481 Account inactive
482 {{ end user.active }}
483  
484 =head2 Structure
485  
486 The template engine expects variables to be provided as hashrefs. Arrays should
487 be provided as arrayrefs. When iterating over arrays:
488  
489 =over 4
490  
491 =item *
492  
493 Arrays of hashrefs: Access hash keys via C<{{ user.name }}>
494  
495 =item *
496  
497 Arrays of simple values: Access values via C<{{ loop.item }}>
498  
499 =back
500  
501 Example data structure:
502  
503 {
504 user => {
505 name => 'Alice',
506 admin => 1,
507 profile => {
508 email => 'alice@example.com'
509 }
510 },
511 items => [
512 'a', 'b', 'c'
513 ],
514 users => [
515 {
516 name => 'Bob',
517 role => 'user'
518 },
519 {
520 name => 'Carol',
521 role => 'admin'
522 }
523 ]
524 }
525  
526 =head2 Context
527  
528 The template engine expects variables to be provided via the L</context>
529 attribute, or as an argument to the L</render> method after the template.
530  
531 Setting context during object construction:
532  
533 package main;
534  
535 use Venus::Template;
536  
537 my $template = Venus::Template->new(context => {email => 'alice@example.com'});
538  
539 $template->render('From: <{{ email }}>');
540  
541 Setting context during template rendering:
542  
543 package main;
544  
545 use Venus::Template;
546  
547 my $template = Venus::Template->new(value => 'From: <{{ email }}>');
548  
549 $template->render(undef, {email => 'alice@example.com'});
550  
551 =cut
552  
553 =head1 ATTRIBUTES
554  
555 This package has the following attributes:
556  
557 =cut
558  
559 =head2 context
560  
561 context(hashref $context) (hashref)
562  
563 The context attribute is read-write, accepts C<(hashref)> values, and is
564 optional.
565  
566 I<Since C<4.15>>
567  
568 =over 4
569  
570 =item context example 1
571  
572 # given: synopsis
573  
574 package main;
575  
576 my $set_context = $template->context({});
577  
578 # {}
579  
580 =back
581  
582 =over 4
583  
584 =item context example 2
585  
586 # given: synopsis
587  
588 # given: example-1 context
589  
590 package main;
591  
592 my $get_context = $template->context;
593  
594 # {}
595  
596 =back
597  
598 =cut
599  
600 =head1 INHERITS
601  
602 This package inherits behaviors from:
603  
604 L<Venus::Kind::Utility>
605  
606 =cut
607  
608 =head1 INTEGRATES
609  
610 This package integrates behaviors from:
611  
612 L<Venus::Role::Accessible>
613  
614 L<Venus::Role::Buildable>
615  
616 L<Venus::Role::Explainable>
617  
618 L<Venus::Role::Valuable>
619  
620 =cut
621  
622 =head1 METHODS
623  
624 This package provides the following methods:
625  
626 =cut
627  
628 =head2 new
629  
630 new(any @args) (Venus::Template)
631  
632 The new method constructs an instance of the package.
633  
634 I<Since C<4.15>>
635  
636 =over 4
637  
638 =item new example 1
639  
640 package main;
641  
642 use Venus::Template;
643  
644 my $new = Venus::Template->new;
645  
646 # bless(..., "Venus::Template")
647  
648 =back
649  
650 =over 4
651  
652 =item new example 2
653  
654 package main;
655  
656 use Venus::Template;
657  
658 my $new = Venus::Template->new('hello world');
659  
660 # bless(..., "Venus::Template")
661  
662 =back
663  
664 =over 4
665  
666 =item new example 3
667  
668 package main;
669  
670 use Venus::Template;
671  
672 my $new = Venus::Template->new(value => 'hello world');
673  
674 # bless(..., "Venus::Template")
675  
676 =back
677  
678 =over 4
679  
680 =item new example 4
681  
682 package main;
683  
684 use Venus::Template;
685  
686 my $new = Venus::Template->new(context => {greeting => 'hello'});
687  
688 # bless(..., "Venus::Template")
689  
690 =back
691  
692 =cut
693  
694 =head2 render
695  
696 render(string $template, hashref $context) (string)
697  
698 The render method processes the template by replacing the tokens and control
699 structurs with the appropriate replacements and returns the result. B<Note:>
700 The rendering process expects variables to be hashrefs and sets (arrayrefs) of
701 hashrefs. Within a C<for> loop, special values are available under the C<loop>
702 variable:
703  
704 =over 4
705  
706 =item *
707  
708 C<{{ loop.index }}> - Zero-based index of current iteration
709  
710 =item *
711  
712 C<{{ loop.place }}> - One-based index of current iteration
713  
714 =item *
715  
716 C<{{ loop.item }}> - Current item value (especially useful for arrays of simple values, e.g. strings)
717  
718 =item *
719  
720 C<{{ loop.parent }}> - Access to parent loop's metadata (if within nested loops)
721  
722 =item *
723  
724 C<{{ loop.level }}> - Access to multi-level parent loop metadata (if within nested loops)
725  
726 =back
727  
728 For nested loops, you can also access specific loop levels using zero-based numbering:
729  
730 =over 4
731  
732 =item *
733  
734 C<{{ loop.level.0 }}> - Top-level loop
735  
736 =item *
737  
738 C<{{ loop.level.1 }}> - One level under the top-level loop
739  
740 =item *
741  
742 C<{{ loop.level.2 }}> - Two levels under the top-level loop
743  
744 =back
745  
746 I<Since C<0.01>>
747  
748 =over 4
749  
750 =item render example 1
751  
752 # given: synopsis;
753  
754 my $result = $template->render;
755  
756 # "From: <>"
757  
758 =back
759  
760 =over 4
761  
762 =item render example 2
763  
764 # given: synopsis;
765  
766 $template->value(
767 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>',
768 );
769  
770 $template->context({
771 email => 'noreply@example.com',
772 });
773  
774 my $result = $template->render;
775  
776 # "From: <noreply@example.com>"
777  
778 =back
779  
780 =over 4
781  
782 =item render example 3
783  
784 # given: synopsis;
785  
786 $template->value(
787 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>',
788 );
789  
790 $template->context({
791 name => 'No-Reply',
792 email => 'noreply@example.com',
793 });
794  
795 my $result = $template->render;
796  
797 # "From: No-Reply <noreply@example.com>"
798  
799 =back
800  
801 =over 4
802  
803 =item render example 4
804  
805 package main;
806  
807 use Venus::Template;
808  
809 my $template = Venus::Template->new(q(
810 {{ for chat.messages }}
811 {{ user.name }}: {{ message }}
812 {{ end chat.messages }}
813 ));
814  
815 $template->context({
816 chat => { messages => [
817 { user => { name => 'user1' }, message => 'ready?' },
818 { user => { name => 'user2' }, message => 'ready!' },
819 { user => { name => 'user1' }, message => 'lets begin!' },
820 ]}
821 });
822  
823 my $result = $template->render;
824  
825 # user1: ready?
826 # user2: ready!
827 # user1: lets begin!
828  
829 =back
830  
831 =over 4
832  
833 =item render example 5
834  
835 package main;
836  
837 use Venus::Template;
838  
839 my $template = Venus::Template->new(q(
840 {{ for chat.messages }}
841 {{ if user.legal }}
842 {{ user.name }} [18+]: {{ message }}
843 {{ else user.legal }}
844 {{ user.name }} [-18]: {{ message }}
845 {{ end user.legal }}
846 {{ end chat.messages }}
847 ));
848  
849 $template->context({
850 chat => { messages => [
851 { user => { name => 'user1', legal => 1 }, message => 'ready?' },
852 { user => { name => 'user2', legal => 0 }, message => 'ready!' },
853 { user => { name => 'user1', legal => 1 }, message => 'lets begin!' },
854 ]}
855 });
856  
857 my $result = $template->render;
858  
859 # user1 [18+]: ready?
860 # user2 [-18]: ready!
861 # user1 [18+]: lets begin!
862  
863 =back
864  
865 =over 4
866  
867 =item render example 6
868  
869 package main;
870  
871 use Venus::Template;
872  
873 my $template = Venus::Template->new(q(
874 {{ for chat.messages }}
875 {{ if user.admin }}@{{ end user.admin }}{{ user.name }}: {{ message }}
876 {{ end chat.messages }}
877 ));
878  
879 $template->context({
880 chat => { messages => [
881 { user => { name => 'user1', admin => 1 }, message => 'ready?' },
882 { user => { name => 'user2', admin => 0 }, message => 'ready!' },
883 { user => { name => 'user1', admin => 1 }, message => 'lets begin!' },
884 ]}
885 });
886  
887 my $result = $template->render;
888  
889 # @user1: ready?
890 # user2: ready!
891 # @user1: lets begin!
892  
893 =back
894  
895 =over 4
896  
897 =item render example 7
898  
899 package main;
900  
901 use Venus::Template;
902  
903 my $template = Venus::Template->new(q(
904 {{ for chat.messages }}
905 [{{ loop.place }}] {{ user.name }}: {{ message }}
906 {{ end chat.messages }}
907 ));
908  
909 $template->context({
910 chat => { messages => [
911 { user => { name => 'user1' }, message => 'ready?' },
912 { user => { name => 'user2' }, message => 'ready!' },
913 { user => { name => 'user1' }, message => 'lets begin!' },
914 ]}
915 });
916  
917 my $result = $template->render;
918  
919 # [1] user1: ready?
920 # [2] user2: ready!
921 # [3] user1: lets begin!
922  
923 =back
924  
925 =over 4
926  
927 =item render example 8
928  
929 package main;
930  
931 use Venus::Template;
932  
933 my $template = Venus::Template->new(q(
934 {{ for chat.messages }}
935 [{{ loop.index }}] {{ user.name }}: {{ message }}
936 {{ end chat.messages }}
937 ));
938  
939 $template->context({
940 chat => { messages => [
941 { user => { name => 'user1' }, message => 'ready?' },
942 { user => { name => 'user2' }, message => 'ready!' },
943 { user => { name => 'user1' }, message => 'lets begin!' },
944 ]}
945 });
946  
947 my $result = $template->render;
948  
949 # [0] user1: ready?
950 # [1] user2: ready!
951 # [2] user1: lets begin!
952  
953 =back
954  
955 =over 4
956  
957 =item render example 9
958  
959 package main;
960  
961 use Venus::Template;
962  
963 my $template = Venus::Template->new(q(
964 {{ for names }}
965 [{{ loop.place }}] {{ loop.item }}
966 {{ end names }}
967 ));
968  
969 $template->context({
970 names => ['user 1', 'user 2', 'user 3'],
971 });
972  
973 my $result = $template->render;
974  
975 # [1] user 1
976 # [2] user 2
977 # [3] user 3
978  
979 =back
980  
981 =over 4
982  
983 =item render example 10
984  
985 package main;
986  
987 use Venus::Template;
988  
989 my $template = Venus::Template->new(q(
990 {{ for teams }}
991 Team {{ loop.item }}:
992 {{ for players }}
993 [{{ loop.place }}] {{ loop.item }} (Team {{ loop.parent.item }})
994 {{ end players }}
995 {{ end teams }}
996 ));
997  
998 $template->context({
999 teams => ['A', 'B'],
1000 players => ['Player 1', 'Player 2'],
1001 });
1002  
1003 my $result = $template->render;
1004  
1005 # Team A:
1006 # [1] Player 1 (Team A)
1007 # [2] Player 2 (Team A)
1008 # Team B:
1009 # [1] Player 1 (Team B)
1010 # [2] Player 2 (Team B)
1011  
1012 =back
1013  
1014 =over 4
1015  
1016 =item render example 11
1017  
1018 package main;
1019  
1020 use Venus::Template;
1021  
1022 my $template = Venus::Template->new(q(
1023 {{ for chapters }}
1024 Chapter {{ loop.level.0.place }}: {{ loop.level.0.item }}
1025 {{ for sections }}
1026 {{ loop.level.1.place }}.{{ loop.level.0.place }} {{ loop.level.0.item }}
1027 {{ end sections }}
1028 {{ end chapters }}
1029 ));
1030  
1031 $template->context({
1032 chapters => ['Intro', 'Methods'],
1033 sections => ['Overview', 'Details']
1034 });
1035  
1036 my $result = $template->render;
1037  
1038 # Chapter 1: Intro
1039 # 1.1 Overview
1040 # 1.2 Details
1041 # Chapter 2: Methods
1042 # 2.1 Overview
1043 # 2.2 Details
1044  
1045 =back
1046  
1047 =cut
1048  
1049 =head1 OPERATORS
1050  
1051 This package overloads the following operators:
1052  
1053 =cut
1054  
1055 =over 4
1056  
1057 =item operation: C<("")>
1058  
1059 This package overloads the C<""> operator.
1060  
1061 B<example 1>
1062  
1063 # given: synopsis;
1064  
1065 my $result = "$template";
1066  
1067 # "From: <>"
1068  
1069 B<example 2>
1070  
1071 # given: synopsis;
1072  
1073 my $result = "$template, $template";
1074  
1075 # "From: <>, From: <>"
1076  
1077 =back
1078  
1079 =over 4
1080  
1081 =item operation: C<(~~)>
1082  
1083 This package overloads the C<~~> operator.
1084  
1085 B<example 1>
1086  
1087 # given: synopsis;
1088  
1089 my $result = $template ~~ 'From: <>';
1090  
1091 # 1
1092  
1093 =back
1094  
1095 =head1 AUTHORS
1096  
1097 Awncorp, C<awncorp@cpan.org>
1098  
1099 =cut
1100  
1101 =head1 LICENSE
1102  
1103 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
1104  
1105 This program is free software, you can redistribute it and/or modify it under
1106 the terms of the Apache license version 2.0.
1107  
1108 =cut
« blib/lib/Venus/Error.pm ↑ Summary blib/lib/Venus/Role/Catchable.pm »