Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> TplParser - Parser szablonów, Klasa, php 4 i 5
treewood
post 10.08.2005, 12:53:01
Post #1





Grupa: Zarejestrowani
Postów: 215
Pomógł: 0
Dołączył: 18.01.2003

Ostrzeżenie: (0%)
-----


Kiedys zrobilem ja dla wlasnego zastosowania i moze przyda sie i Wam.
Dostepna jest takze przy projekcie Quick.Cart i Quick.Forum dostepnych na stronie OpenSolution.org

Klasa/plik: "TplParser.php"

Kod php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php

/**
* @access public
* @version 1.1.6-bf2
* @date 2004-12-27 15:51:47
*/
class TplParser
{

    var $content;
    var $TplFile;
    var $block;
    var $startBlock;
    var $endBlock;
  var $endBlockLine;
  var $fileContent;    
  var $blockContent
;
  var $cache;
  var $iso;
  var $directory;

  /**
  * Konstruktor
  */
    function TplParser( ){
        $this->content           = ' ';
        $this->TplFile           = '';
        $this->block              = '';
        $this->startBlock   = '<!-- BEGIN ';
        $this->endBlock       = '<!-- END ';
    $this->endBlockLine = ' -->';
    $this->cache        = true;
    $this->iso          = true;
    } // end function TplParser
    
  
/**
  * Wyswietlenie calego sparsowanego pliku
  * @return void
  * @param string $sFile - plik *.tpl
  * @param bool   $bCache
  * @param bool   $bIso
  */
    function dHtml( $sFile, $bCache = true, $bIso = true ){
        $this->setFile( $this->directory.$sFile );
    $this->cache =    $bCache;
    $this->iso =      $bIso;

        $this->display( );
    echo $this->content;
    unset( $this->content );
        flush( );
    } // end function dHtml

  /**
  * Zwrocenie calego sparsowanego pliku
  * @return string
  * @param string $sFile - plik *.tpl
  * @param bool   $bCache
  * @param bool   $bIso
  */
    function tHtml( $sFile, $bCache = true, $bIso = true ){
        $this->setFile( $this->directory.$sFile );
    $this->cache =    $bCache;
    $this->iso =      $bIso;

        $this->display( );
        return $this->content;
    } // end function tHtml

  /**
  * Wyswietlenie sparsowanego bloku w pliku
  * @return void
  * @param string $sFile - plik *.tpl
  * @param string $sBlock
  * @param bool   $bCache
  * @param bool   $bIso
  */
    function dbHtml( $sFile, $sBlock, $bCache = true, $bIso = true ){
        $this->setFile( $this->directory.$sFile );
        $this->setBlock( $sBlock );

    $this->cache =    $bCache;
    $this->iso =      $bIso;

        $this->display( true );
    echo $this->content;
    unset( $this->content );
        flush( );
    } // end function dbHtml
    
  
/**
  * Zwrocenie sparsowanego bloku w pliku
  * @return string
  * @param string $sFile - plik *.tpl
  * @param string $block
  * @param bool   $cache
  * @param bool   $iso
  */
    function tbHtml( $sFile, $sBlock, $bCache = true, $bIso = true ){
        $this->setFile( $this->directory.$sFile );
        $this->setBlock( $sBlock );

    $this->cache =    $bCache;
    $this->iso =      $bIso;

        $this->display( true );
        return $this->content;
    } // end function tbHtml

  /**
  * Wykonanie czynnosci na kodzie HTML
  * @return void
  * @param bool $bBlock [optional]
  */
    function display( $bBlock = null ){
        if( $this->checkFile( ) ){
            if( isset( $bBlock ) )
                $this->blockParse( );
            else
                $this
->allParse( );
            
            $this
->changeTxt( );
        }
    } // end function display
    
    
/**
    * Zmiana polskich liter na znaki ISO-8859-2
    * @return void
    */
    function changeTxt( ){
    if( $this->iso == true ){
      $aStr[] = '/ś/';
      $aStr[] = '/ą/';
      $aStr[] = '/Ś/';
      $aStr[] = '/ź/';
      $aStr[] = '/Ą/';
      $aStr[] = '/Ź/';

      $aRep[] = 'ś';
      $aRep[] = 'ą';
      $aRep[] = 'Ś';
      $aRep[] = 'Ľ';
      $aRep[] = 'ˇ';
      $aRep[] = 'Ź';
      $this->content = preg_replace( $aStr, $aRep, $this->content );
    }
    } // end function changeTxt
    
    
/**
  * Sprawdzenie istnienia pliku
  * @return boolean
  */
    function checkFile( ){
        if( is_file( $this->TplFile ) ){
          return true;
      }
        else {
      $this->content = null;
            echo 'No template file: <i>'.$this->TplFile.'</i><br />';
            return false;
        }
    } // end function checkFile
    
  
/**
  * Parsowanie zawartosci pliku
  * @return boolean
  */
    function parse( ){

        if( !isset( $poz ) )
      $poz[1][1] = 0;
    
        while
( strpos( $this->content, '$', $poz[1][1] ) )  {
            $poz[1][1] = strpos( $this->content, '$', $poz[1][1] ) + 1;

            preg_match( '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]x7f-\xff]*/', $this->content, $wyniki );

            if( ereg( '\]', $wyniki[0] ) ){
        if( ereg( '\[', $wyniki[0] ) ){
                  $poz[1][2] = $poz[1][1] + strpos( $wyniki[0], ']' );
          $bArray = true;
        }
        else{
          $poz[1][2] = $poz[1][1] + strpos( $wyniki[0], ']' ) - 1;
          $bArray = null;
        }
      }
            else{
                $poz[1][2] = $poz[1][1] + strlen( $wyniki[0] ) - 1;
        $bArray = null;
      }

            $TplVar = substr( $this->content, $poz[1][1], $poz[1][2] - $poz[1][1] );

            if( isset( $bArray ) ){
                list($var,) = explode( '[', $TplVar );
        if( isset( $GLOBALS[$var] ) ){
                  global $$var;
          $poz[2][1]= strpos( $TplVar, '[' )+1;
          $poz[2][2]= strpos( $TplVar, ']' );
          $key = substr( $TplVar, $poz[2][1], $poz[2][2]-$poz[2][1] );
          
          if
( isset( ${$var}[$key] ) )
            $tekst = ${$var}[$key];
          else
            $tekst 
= null;
        }
        else{
          $tekst = null;
        }
            } 
            else 
{
                global $$TplVar;
            $tekst = $$TplVar;
            }
            $this->content = substr( $this->content, 0, $poz[1][1] - 1 ) . $tekst .  substr( $this->content, $poz[1][2] );
        } // end while
        
        $this
->content = substr( $this->content, 0 );
        return true;
        
    
} // end function parse
    
  
/**
  * Pobranie wszystkich danych z pliku
  * @return void
  */
    function allParse( ){
    if( isset( $this->fileContent[$this->TplFile] ) ){
      $this->content = $this->fileContent[$this->TplFile];
    }
    else{
      $this->content = $this->getContent( );
    }
        $this->parse( );
    } // end function allParse
    
  
/**
  * Pobranie wybranych danych z pliku
  * i parsowanie
  * @return boolean
  */
    function blockParse( ){
   
    if
( isset( $this->blockContent[$this->TplFile][$this->block] ) ){
      $this->content = $this->blockContent[$this->TplFile][$this->block];
    }
    else{
      $this->content = $this->getFileBlock( );
      if( isset( $this->content ) ){
        if( $this->cache == true ){
          $this->blockContent[$this->TplFile][$this->block] = $this->content;
        }
      }
    }
    $this->parse( );
    } // end function blockParse

  /**
  * Otwieranie pliku i jego zawartosci 
  * lub pobieranie ze zmiennej w przypadku cashe'owania
  * @return array
  * @param bool $bBlock
  */
  function getContent( $bBlock = null ){
    if( isset( $this->fileContent[$this->TplFile] ) ){
      $mReturn = $this->fileContent[$this->TplFile];
    }
    else{
      if( isset( $bBlock ) ){
        $mReturn = $this->getFile( $this->TplFile );
      }
      else{
        $mReturn = $this->getFile( $this->TplFile );
      }

      if( $this->cache == true )
        $this->fileContent[$this->TplFile] = $mReturn;
    }
    return $mReturn;
  } // end function getContent

  /**
  * Zwracanie zawartosci pliku do jednej zmiennej
  * metoda moze byc uzywana z zewnatrz
  * @return string
  * @param string $sFile
  */
  function getFile( $sFile ){
    $rFile =  fopen( $sFile, 
Ten post edytował treewood 10.03.2006, 12:27:40

--------------------
Działam w OpenSolution.org, autor Quick.Cms i Quick.Cart już od ponad 10 lat
Go to the top of the page
+Quote Post
FiDO
post 12.08.2005, 15:03:08
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 717
Pomógł: 0
Dołączył: 12.06.2002
Skąd: Wolsztyn..... Studia: Zielona Góra

Ostrzeżenie: (0%)
-----


Prosze o poprawienie tematu na zgodny z tym dzialem smile.gif
http://forum.php.pl/index.php?showtopic=30979


--------------------
Brak czasu :/
Go to the top of the page
+Quote Post
Bakus
post 17.08.2005, 00:16:45
Post #3


Administrator serwera


Grupa: Przyjaciele php.pl
Postów: 909
Pomógł: 0
Dołączył: 12.08.2003
Skąd: /var/www/wroclaw.php

Ostrzeżenie: (0%)
-----


FiDO: Czepiasz sie... smile.gif


--------------------
Powrót do przeszłości :)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 

RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 04:18