Thread Template Include (49 answers)
Opened by tomlong at 2004-04-04 20:44

Gast Gast
 2004-06-06 20:38
#1937 #1937
Ich hab da jetzt mal was (ganz kurzes) gemacht.
Wäre nett wenn sich das mal jemand ansehen und auf Fehler prüfen könnte (ohne mich dabei bis auf die Knochen zu blamieren) ;)
Das Ding ist hier wieder einmal sehr schlecht formatiert - Kommodo ist ein feines Tool für die Programmierung, aber nicht so besonders nett zu copy&paste :(

Code: (dl )
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
#-#############################################
# Template.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.
#
# eTPL Release Version 1.2.1 (06/06/2004)
# Copyright (C) 2004 Dieter Werner
# All rights reserved by the author.
#
#-#############################################
# OK - Here we go ...

   package Template;

#-#############################################
# 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'} = {

   }
            );
}

#-#############################################
# 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;


Das Teil erlaubt die wahlfreie Verwendung von wie auch immer benannten (eigenen) HTML-Tags innerhalb der Template-Datei

<TPL_MyTag Name="MyName">
<Echter HTML-Code>
</TPL_MyTag Name="MyName">

Innerhalb des Scripts läuft es dann so ...

Variablen innerhalb von benanten TPL-Tags:
$obj->tpl_param(
   'TPL_MyTag',
   'MyName',
   {
     'var_1' => $cont_1,
     'var_2' => $cont_2,
     'var_n' => $cont_n,
   }
);

damit lassen sich dann auch externe Dateien einbinden oder es kann interner/externer Code ausgeführt werden.

Variablen außerhalb von benanten TPL-Tags:
$obj->tpl_param(
   undef,
   undef,
   {
     'var_1' => $cont_1,
     'var_2' => $cont_2,
     'var_n' => $cont_n,
   }
);

Um einen Loop-Content zu erzeugen lautet der Code im Script:
   if ($foo == $bar) {
       $obj->tpl_param(
           'TPL_MyTag',
           'MyName',
           {
               'var_1' => $cont_1,
               'var_2' => $cont_2,
               'var_n' => $cont_n,
           }
       )
   }

und so weiter ...

Zu ersetzende Variablen werden im Template mit
<TPL_VAR='var_x'>
beschrieben.

Zu ersetzender Text (z.B. bei mehrsprachigen Programmen) wird im Template mit
<TXT='What Ever'>
beschrieben.

View full thread Template Include