File Coverage

blib/lib/DTL/Fast/Tag/Block.pm
Criterion Covered Total %
statement 38 40 95.0
branch 9 10 90.0
condition n/a
subroutine 8 8 100.0
pod 0 3 0.0
total 55 61 90.1


line stmt bran cond sub pod time code
1             package DTL::Fast::Tag::Block;
2 2     2   1426 use strict; use utf8; use warnings FATAL => 'all';
  2     2   4  
  2     2   52  
  2         10  
  2         3  
  2         11  
  2         48  
  2         4  
  2         67  
3 2     2   10 use parent 'DTL::Fast::Tag';
  2         3  
  2         11  
4              
5 2     2   105 use Data::Dumper;
  2         3  
  2         1013  
6              
7             our $VERSION = '1.00';
8              
9             $DTL::Fast::TAG_HANDLERS{'block'} = __PACKAGE__;
10              
11             #@Override
12 27     27 0 85 sub get_close_tag{return 'endblock';}
13              
14             #@Override
15             sub parse_parameters
16             {
17 26     26 0 32 my( $self ) = @_;
18              
19 26         52 $self->{'block_name'} = $self->{'parameter'};
20            
21 26 100       66 die $self->get_parse_error("no name specified in the block tag") if not $self->{'block_name'};
22              
23             # registering block within template
24 25 100       58 if ( exists $DTL::Fast::Template::CURRENT_TEMPLATE->{'blocks'}->{$self->{'block_name'}} )
25             {
26             die $self->get_parse_error(
27             sprintf(
28             "block name `%s` must be unique in the template"
29             , $self->{'block_name'}
30             )
31             , 'Reason' => sprintf(
32             'block `%s` was already defined at line %s'
33             , $self->{'block_name'}
34 1         9 , $DTL::Fast::Template::CURRENT_TEMPLATE->{'blocks'}->{$self->{'block_name'}}->{'_template_line'}
35             )
36             );
37             }
38             else
39             {
40 24         61 $DTL::Fast::Template::CURRENT_TEMPLATE->{'blocks'}->{$self->{'block_name'}} = $self;
41             }
42            
43 24         49 return $self;
44             }
45              
46             #@Override
47             sub render
48             {
49 24     24 0 39 my( $self, $context ) = @_;
50            
51 24         29 my $result;
52            
53 24         68 $context->push_scope();
54 24         44 my $ns = $context->{'ns'}->[-1];
55            
56 24         80 $ns->{'_dtl_rendering_block'} = $self;
57            
58 24 100       52 if ( $ns->{'_dtl_descendants'} )
59             {
60             # template with inheritance
61 16         20 foreach my $descendant (@{$ns->{'_dtl_descendants'}})
  16         32  
62             {
63 24 50       98 if ( $descendant == $self )
    100          
64             {
65 0         0 $result = $self->SUPER::render($context);
66 0         0 last;
67             }
68             elsif($descendant->{'blocks'}->{$self->{'block_name'}})
69             {
70 16         21 $ns->{'_dtl_rendering_template'} = $descendant;
71 16         52 $result = $descendant->{'blocks'}->{$self->{'block_name'}}->SUPER::render($context);
72 16         31 last;
73             }
74             }
75             }
76             else
77             {
78             # simple template
79 8         31 $result = $self->SUPER::render($context);
80             }
81 18         58 $context->pop_scope();
82            
83 18         87 return $result;
84             }
85              
86             1;