NetSDS/Template.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« NetSDS/Session.pm ↑ Summary NetSDS/App.pm »
1 #===============================================================================
2 #
3 # FILE: Template.pm
4 #
5 # DESCRIPTION: Wrapper for HTML::Template::Pro
6 #
7 # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
8 # COMPANY: Net.Style
9 # CREATED: 16.07.2008 23:30:12 EEST
10 #===============================================================================
11  
12 =head1 NAME
13  
14 NetSDS::Template - NetSDS template engine
15  
16 =head1 SYNOPSIS
17  
18 use NetSDS::Template;
19  
20 =head1 DESCRIPTION
21  
22 C<NetSDS::Template> class provides developers with ability to create template
23 based web applications.
24  
25 =cut
26  
27 package NetSDS::Template;
28  
29 2 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use base 'NetSDS::Class::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use HTML::Template::Pro;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 0 ▶use NetSDS::Util::File;
Subroutine BEGIN: called CC=1 SCAR=2.6
37  
38 use version; our $VERSION = '1.301';
39  
40 #===============================================================================
41  
42 =head1 CLASS API
43  
44 =over
45  
46 =item B<new([%params])> - class constructor
47  
48 This concstructor loads template files and parse them.
49  
50 my $tpl = NetSDS::Template->new(
51 dir => '/etc/NetSDS/templates',
52 esc => 'URL',
53 include_path => '/mnt/floppy/templates',
54 );
55  
56 =cut
57  
58 #-----------------------------------------------------------------------
59 sub new {
60  
61 my ( $class, %params ) = @_;
62  
63 # Get filenames of templates
64 my $tpl_dir = $params{dir};
65  
66 # Initialize templates hash reference
67 my $tpl = {};
68  
69 my @tpl_files = @{ dir_read( $tpl_dir, 'tmpl' ) };
70  
71 # Add support for 'include_path' option
72 foreach my $file (@tpl_files) {
73  
74 # Read file to memory
75 if ( $file =~ /(^.*)\.tmpl$/ ) {
76  
77 # Determine template name and read file content
78 my $tpl_name = $1;
79 my $tpl_content = file_read("$tpl_dir/$file");
80  
81 if ($tpl_content) {
82  
83 # Prepare include path list
84 my $include_path = $params{include_path} ? $params{include_path} : undef;
85 my @inc = ();
86 if ( defined $include_path ) { push @inc, $include_path; }
87 push @inc, ( $params{dir} . '/inc', '/usr/share/NetSDS/templates/' );
88  
89 # Create template processing object
90 my $tem = HTML::Template::Pro->new(
91 scalarref => \$tpl_content,
92 filter => $params{filter} || [],
93 loop_context_vars => 1,
94 global_vars => 1,
95 default_escape => defined( $params{esc} ) ? $params{esc} : 'HTML',
96 search_path_on_include => 1,
97 path => \@inc,
98 );
99  
100 $tpl->{$tpl_name} = $tem;
101 } ## end if ($tpl_content)
102  
103 } ## end if ( $file =~ /(^.*)\.tmpl$/)
104  
105 } ## end foreach my $file (@tpl_files)
106  
107 # Create myself at last :)
108 return $class->SUPER::new( templates => $tpl );
109  
110 } ## end sub new
111  
112 #***********************************************************************
113  
114 =item B<render($tmpl_name, %params)> - render document by template and paramters
115  
116 This method prepares set of parameters and applies them to given template.
117 Return is a ready for output document after processing.
118  
119 Example:
120  
121 # Simple template rendering with scalar parameters
122 my $str = $tmp->render('main', title => 'Main Page');
123  
124 # Rendering template with array parameters
125 my $str2 = $tmp->render('list', title => 'Statistics', users => \@users_list);
126  
127 =cut
128  
129 #-----------------------------------------------------------------------
130  
131 sub render {
132  
133 my ( $self, $name, %params ) = @_;
134  
135 my $tpl = $self->{'templates'}->{$name};
136  
137 unless ($tpl) {
138 return $self->error("Wrong template name: '$name'");
139 }
140  
141 # Clear previously set paramters and change to new
142 $tpl->clear_params();
143 $tpl->param(%params);
144  
145 # Return finally rendered document
146 return $tpl->output;
147 }
148  
149 1;
150  
151 __END__
« NetSDS/Session.pm ↑ Summary NetSDS/App.pm »