Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z Parserem....
Forum PHP.pl > Forum > PHP
keedy
mam taką funckje:

  1. <?php
  2.  
  3. public function display( $strFile ) {
  4. $resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
  5. $strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
  6. foreach( $this->arrVars as $strVar => $strValue ) {
  7. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  8. }
  9. echo $strOutput;
  10. }?>


Otóz jak widac zmienna $strOutput jest za kazdą petlą foreach jest nadpisywana, jak rozwiązac ten problem, aby zmienna nei byla nadpisywana?
czachor
Zrobić z tego tablicę...?
keedy
Moze dla ułatwienia podam cały kod:
  1. <?php
  2.  
  3. class Template {
  4.  
  5. public $strTemplateDir = './templates/2';
  6. public $strBeginTag = '{';
  7. public $strEndTag = '}';
  8. public $arrVars = array();
  9.  
  10. private function __contruct() {}
  11.  
  12. public function assign( $strVar, $strValue ) {
  13. $this->arrVars[$strVar] = $strValue;
  14. }
  15.  
  16. public function display( $strFile ) {
  17. $resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
  18. $strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
  19. foreach( $this->arrVars as $strVar => $strValue ) {
  20. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  21. }
  22. echo $strOutput;
  23. }
  24. }
  25.  
  26. $tpl = new Template;
  27. $tpl->assign( 'Time', 'test' );
  28. $tpl->assign( 'Dwa', 'test-2' );
  29. $tpl->assign( 'Dwaq', 'test-2' );
  30. echo $tpl->display( 'index.tpl' );
  31. ?>
SmokU
  1. <?php
  2.  
  3. foreach( $this->arrVars as $strVar => $strValue ) {
  4. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  5. }
  6. echo $strOutput;
  7.  
  8. ?>


zamień na

  1. <?php
  2.  
  3. foreach( $this->arrVars as $strVar => $strValue ) {
  4. $strOutput .= str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  5. }
  6. echo $strOutput;
  7.  
  8. ?>


Zmiany dokonane:

$strOutput = zamienione na $strOutput .= . Tworzy to tablicę i dane są dopisywane do zmiennej a nie jak z przypadku bez kropki nadpisywane nowe na stare...
keedy
tak tylko wtedy caly tpl jest wyswietlany tyle razy ile zadeklarowano "zmiennych" w tym wypadku 3...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.