Leser: 9
![]() |
|< 1 2 3 4 5 >| | ![]() |
50 Einträge, 5 Seiten |
Template->new->process("templatefile", { array => \@array })
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
###############
package main;
###############
BEGIN {
print "Content-type: text/html\n\n";
$0 =~ m~(.*)/[^/]+~ || $0 =~ m~(.*)\\[^\\]+~;
unshift @INC, $1;
}
use strict;
use warnings;
my $token = EasyTemplate->new();
my @array = qw/Dieser Text besteht aus den Elementen eines Arrays/;
# Loop
$token->tpl_param(
'TPL_TEST1',
'Loop_test',
{ 'foo' => $_ }
) foreach @array;
# Conditions
foreach (@array) {
$token->tpl_param(
'TPL_TEST2',
'If_test',
{ 'foo' => $_ }
) if $_ eq 'Elementen';
}
# Include
$token->tpl_param(
'TPL_TEST3',
'Include_test',
{
'foo' => foo(),
'bar' => bar()
}
);
$token->disp_template('test_file');
###############
sub foo {
###############
return 'Rueckgabewert aus sub foo';
}
###############
sub bar {
###############
return 'Rueckgabewert aus sub bar';
}
#-##############################################
# EasyTemplate.pm
# Date: 05/15/2004
# Version: eTPL-1.2.1
# Last update: 06/06/2004
#-#############################################
# Best code-viewing with 'Komodo' by ActiveState
# http://www.activestate.com
#-##############################################
# LICENSE-CONDITIONS:
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#-#############################################
#
# In accordance with the GPL, each copyright notice MUST remain intact.
#
# EasyTemplate Release Version 1.2.1 (06/06/2004)
# Copyright (C) 2004 Dieter Werner
# All rights reserved by the author.
#
#-#############################################
package EasyTemplate;
#-#############################################
# Use-Section
#-#############################################
use strict;
use warnings;
#-#############################################
# Constructor
#-#############################################
sub new {
my $invoc = shift;
my $attrib = shift || undef;
my $class = ref($invoc) || $invoc;
my $self = {};
bless($self, $class);
return($self->init_template($attrib));
}
#-#############################################
# Define properties
#-#############################################
sub init_template {
my $self = shift;
my $attrib = shift;
local $_;
$attrib
? do {
($self->{'template'}->{$_} = $attrib->{$_})
foreach (keys %$attrib)
}
: ($self->{'template'} = {
}
);
return $self;
}
#-#############################################
# Sub: Get parameters
#-#############################################
sub tpl_param {
my ($self, $tag, $id, $param) = @_;
(!defined $tag and defined $id) && $self->oops('Template-Tag not defined');
(defined $tag and !defined $id) && $self->oops('Tag-Name not defined');
($tag, $id) = (
'TPL_COMMON',
'Common'
) unless defined $tag and defined $id;
push @{$self->{'template'}->{'param'}->{$tag}->{$id}}, $param;
}
#-#############################################
# Sub: Read and prepare a template file
#-#############################################
sub disp_template {
my ($self, $file) = @_;
my ($tag, $id);
local $_;
local $/;
open FILE, "./Template/$file.txt" or $self->oops('Template-File not found');
$self->{'template'}->{'cont'} .= <FILE>;
close FILE;
foreach $tag (keys %{$self->{'template'}->{'param'}}) {
while ($self->{'template'}->{'cont'} =~ /\<$tag\sName\=\"(\w+)\"\>(.*?)\<\/$tag\sName\=\"\w+\"\>/sg) {
$self->{'template'}->{$tag}->{$1} = $2;
$self->{'template'}->{'cont'}
=~ s/\<$tag\sName\=\"($1)\"\>.*?\<\/$tag\sName\=\"$1\"\>/\<$tag\=\"$1\"\>/s;
}
}
_replace_content($self);
}
#-#############################################
# Sub: Replace named contents and global variables
#-#############################################
sub _replace_content {
my $self = shift;
my ($tag, $id, $subst, $temp, $cache);
local $_;
foreach $tag (keys %{$self->{'template'}->{'param'}}) {
$tag eq 'TPL_COMMON' && next;
foreach $id (keys %{$self->{'template'}->{'param'}->{$tag}}) {
foreach $subst (@{$self->{'template'}->{'param'}->{$tag}->{$id}}) {
$temp = $self->{'template'}->{$tag}->{$id} || next;
$temp =~ s/\<TPL_VAR\=\'$_\'\>/$subst->{$_}/sg
foreach (keys %$subst);
$cache .= $temp;
}
$self->{'template'}->{'cont'} =~ s/\<$tag\=\"$id\"\>/$cache/s;
undef $cache;
}
}
foreach $subst (@{$self->{'template'}->{'param'}->{'TPL_COMMON'}->{'Common'}}) {
$self->{'template'}->{'cont'} =~ s/\<TPL_VAR\=\'$_\'\>/$subst->{$_}/sg
foreach (keys %$subst);
}
_replace_txt($self);
}
#-#############################################
# Sub: Replace text-content (Translated text)
#-#############################################
sub _replace_txt {
my $self = shift;
my $err_message = '<b>Text-Replacement is not available</b>';
_wipe_template($self)
unless exists $self->{'txt'} or exists $self->{'long_txt'};
$self->{'template'}->{'cont'}
=~ s/\<TXT\=\'(\w+.*?)\'\>/$self->{'txt'}->{$1} || $self->{'long_txt'}->{$1} || $err_message/esg;
_wipe_template($self);
}
#-#############################################
# Sub: Delete unused stuff
#-#############################################
sub _wipe_template {
my $self = shift;
$self->{'template'}->{'cont'} =~ s/\<TPL_.*?\/TPL_\w+\sName\=\"\w+\"\>\r*\n//sg;
$self->{'template'}->{'cont'} =~ s/\<TPL_\w+\=\"\w+\"\>\r*\n//sg;
_print_template($self);
}
#-#############################################
# Sub: Display the template
#-#############################################
sub _print_template {
my $self = shift;
print $self->{'template'}->{'cont'};
delete $self->{'template'};
}
#-###########################################-#
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
<head>
<title>Test</title>
</head>
<body>
<div align="center">
<table bgcolor="FFFFFF" width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="red"> </td>
</tr>
<TPL_TEST1 Name="Loop_test">
<tr>
<td nowrap><TPL_VAR='foo'></td>
</tr>
</TPL_TEST1 Name="Loop_test">
<tr>
<td bgcolor="red"> </td>
</tr>
<TPL_TEST2 Name="If_test">
<tr>
<td nowrap><TPL_VAR='foo'></td>
</tr>
</TPL_TEST2 Name="If_test">
<tr>
<td bgcolor="red"> </td>
</tr>
<TPL_TEST3 Name="Include_test">
<tr>
<td nowrap><TPL_VAR='foo'></td>
</tr>
<tr>
<td nowrap><TPL_VAR='bar'></td>
</tr>
</TPL_TEST3 Name="Include_test">
<tr>
<td bgcolor="red"> </td>
</tr>
</table>
</div>
</body>
</html>
QuoteOb 'sich schon im Code Gedanken' über die Repräsentation machen zu müssen von 'Nachteil' ist - wage ich zu bezweifeln ;)
QuoteIch bin wohl kein Normalverbraucher :-)Was meinst Du mit 'eingeschränkte Sache' ?
Bei richtiger Anwendung macht das Teil (mit den paar Zeilen Code) alles was man so als 'Normalverbraucher' von einem Template erwartet.
my ($self, $tag, $id, $param) = @_
![]() |
|< 1 2 3 4 5 >| | ![]() |
50 Einträge, 5 Seiten |