-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTmpl.pm
137 lines (111 loc) · 2.73 KB
/
Tmpl.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# Copyright (C) 2008 - 2009 Nano-opt
# Licence: GPL
package Tmpl;
use strict;
use base qw(Exporter);
use Template;
use Constants;
use LMConfig;
use Utils;
%Template::EXPORT = qw(
new
process
get_default_type
set_vars
vars
throw_error_code
throw_error_user
);
our $conf_template;
our %hash_vars = {};
sub new {
my ($self) = @_;
$conf_template = {
INCLUDE_PATH => Constants::LOCATIONS()->{'templates'},
INTERPOLATE => 1,
POST_CHOMP => 0,
EVAL_PERL => 1,
COMPILE_DIR => Constants::LOCATIONS()->{'compile_t'},
PRE_PROCESS => 'initialize.none.tmpl',
ENCODING => 'UTF-8',
FILTERS => {
none => \&Utils::filter_none,
js => \&Utils::filter_js,
html_lb => \&Utils::filter_html_lb,
html_nb => \&Utils::filter_html_nb,
html => \&Utils::filter_html,
text => \&Utils::filter_text,
url_quote => \&Utils::filter_url_quote,
},
CONSTANTS => _load_constants(),
VARIABLES => {
},
};
# $conf_template->{DEBUG} => 'parser, undef';
return $self;
}
sub process {
my ($self, $template, $type, $out) = @_;
my $obj_template = Template->new($conf_template);
if (defined($type) && ($type ne '')) {
$template .= '.' . $type . '.tmpl';
} else {
$template .= '.' . $self->get_default_type() . '.tmpl';
}
my $ret = $obj_template->process($template, $self->vars(), $out);
return $ret;
}
sub set_vars {
my ($self, $name, $value) = @_;
$hash_vars{$name} = $value;
}
sub vars {
my ($self) = @_;
return \%hash_vars;
}
sub get_default_type {
my ($self) = @_;
my $type = 'html';
if (! Utils::exec_from_cgi()) {
$type = 'txt';
}
return $type;
}
sub throw_error_code {
my ($self, $err_id) = @_;
$self->_throw_error('error/code', $err_id);
}
sub throw_error_user {
my ($self, $err_id) = @_;
$self->_throw_error('error/user', $err_id);
}
################################################################## PRIVATE
sub _throw_error {
my ($self, $fname, $err_id) = @_;
$self->set_vars('error', $err_id);
$self->process($fname);
exit;
}
sub _load_constants() {
my %consts;
foreach my $item (@Constants::EXPORT) {
if (ref Constants->$item) {
$consts{$item} = Constants->$item;
} else {
my @list = (Constants->$item);
$consts{$item} = (scalar(@list) == 1) ? $list[0] : \@list;
}
}
return \%consts;
}
1;
__END__
=head1 NAME
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 METHODS
=over
=item C<>
=back