File Coverage

blib/lib/Bricklayer/Templater/Handler.pm
Criterion Covered Total %
statement 18 19 94.7
branch 5 6 83.3
condition n/a
subroutine 10 10 100.0
pod 9 9 100.0
total 42 44 95.4


line stmt bran cond sub pod time code
1             # Bricklayer Plugin SuperClass
2             package Bricklayer::Templater::Handler;
3              
4 3     3   2987 use Carp;
  3         4  
  3         1157  
5              
6             =head1 NAME
7              
8             Bricklayer::Templater::Handler - Base class for all Template tag handlers
9              
10             =head1 SYNOPSIS
11              
12             =head1 DESCRIPTION
13              
14             Bricklayer::Templater::Handler does all the common heavy lifting that a Template tag handler needs to do. It initialize the handler object and sets up the callback hooks that are needed.
15              
16             =head2 The Handler API.
17              
18             =head3 Handler object attributes
19              
20             =over 1
21              
22             =item app
23            
24             $self->app() returns the template engine
25              
26             =item attributes
27            
28             $self->attributes() returns a hashref of tag attributes. tag attributes are specified the same way
29             that xml attributes are with the following restrictions.
30              
31             =over 2
32              
33             =item attribute values must be enclosed in double quotes
34              
35             =item
36              
37             =back
38              
39             =item block
40            
41             $self->block() returns the text block contents of the tag. The bricklayer parser is not recursive
42             on its own. If you tag is expected to process the block as more template text then you should call
43             parse_block() as shown below.
44            
45             =item type
46            
47             $self->type() returns the tag type. This shoule be either 'block' or 'single' or 'text'. You
48             probably will never need to use this.
49              
50             =item tagname
51            
52             $self->tagname() returns the tag's name. This should be the classname minus the
53             Bricklayer::Templater::Handler:: part.
54              
55             =item tagid
56            
57             $self->tagid() returns the template engines current template tag identifier. It is equivalent to
58             calling $self->app()->identifier.
59              
60             =back
61              
62             =head3 handler object Methods
63              
64             =over 1
65              
66             =item load
67              
68             Bricklayer::Templater::Handler::tag::name->load() is the constructor for a tag handler.
69             it will get passed a Token data structure and the Template engine object as a context.
70            
71             =item parse_block
72              
73             $self->parse_block($arg) is a convenience function that will run the templater on the block with
74             any argument you pass in to it.
75              
76             =item run_handler
77              
78             $handler->run_handler() is what the engine calls to actually run the handler. You probably
79             shouldn't be using this method since it's mostly for internal use.
80              
81             =back
82              
83             =cut
84              
85             # Initialization
86             sub load {
87 7     7 1 6793 my $PluginObj = {Token => $_[1],
88             App => $_[2],
89             err => undef
90             };
91            
92 7 100       34 croak "ahhh didn't get passed the Token object" unless $_[1];
93 6 100       25 croak "ahhh didn't get passed the context object" unless $_[2];
94 5         14 $PluginObj = bless($PluginObj, $_[0]);
95              
96 5 50       44 $PluginObj->load_extra()
97             if $PluginObj->can('load_extra'); # optional method for handlers
98            
99 5         17 return $PluginObj;
100             }
101              
102             sub attributes {
103 1     1 1 7 return $_[0]->{Token}{attributes};
104             }
105              
106             sub block {
107 2     2 1 18 return $_[0]->{Token}{block};
108             }
109              
110             sub type {
111 1     1 1 530 return $_[0]->{Token}{type};
112             }
113              
114             sub tagname {
115 1     1 1 9 return $_[0]->{Token}{tagname};
116             }
117              
118             sub tagid {
119 1     1 1 4 return $_[0]->app()->identifier();
120             }
121              
122             sub app {
123 9     9 1 1384 return $_[0]->{App};
124             }
125              
126             sub parse_block {
127 1     1 1 407 $_[0]->app->run_sequencer($_[0]->block(), $_[1]);
128 0         0 return ;
129             }
130              
131             sub run_handler {
132 4     4 1 16 my $result = $_[0]->run($_[1]);
133 4         10 $_[0]->app()->publish($result);
134             }
135              
136             return 1;