Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Własny parser szablonow
Forum PHP.pl > Forum > Przedszkole
axel_pl
Witajcie, proszę o pomoc z pętlami, mój parser wygląda tak:
class/parser.class.php
  1. <?php
  2. class Template{
  3.  
  4.  var $sSource;
  5.  var $sParsed;
  6.  var $aValues = array();
  7.  
  8.  function load($sTemplate) {
  9. return (!$this->sSource = @file_get_contents($sTemplate)) ? false : true;
  10.  }
  11.  
  12.  
  13.  function init($sKey, $sValue) {
  14. $this->aValues[$sKey] = $sValue;
  15.  }
  16.  
  17.  function run() {
  18. print preg_replace('/{(.+?)}/e', '$this->aValues['1']', $this->sSource);
  19.  }
  20. } 
  21. ?>


plik strony głównej tak
  1. <?
  2. include('config.php');
  3. include('inc/news.inc.php');
  4. $tpl = new Template;
  5. $tpl -> load(TPL_DIR.'index.tpl');
  6. $tpl -> init('{categorie}', category($id));
  7. $tpl -> run();
  8. ?>

a plik inc/news.inc.php tak:
  1. <?php
  2. if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) exit(' dostęp zabroniony!');
  3.  
  4. //...skrypt
  5.  
  6. function category($id) {
  7. global $artSQL, $user, $tpl, $db;
  8. $id = (int) $id;
  9. $get = "select art_id, art_title, art_text, user_id, art_comments
  10. from $artSQL
  11. where
  12. art_topic = '$id'
  13. order by
  14. art_data
  15. asc";
  16. $query = $db->query($get);
  17. $num = $db->num_rows($query);
  18. if($num > 0) {
  19. while($obj = $db->fetch_object($query)) {
  20. $tpl -> load(TPL_DIR.'art/art_in_cat.tpl');
  21. $tpl -> init('{title}', $obj->art_title);
  22. $tpl -> init('{text}', $obj->art_text);
  23. $tpl -> init('{poster}', $user->username($obj->user_id));
  24. $tpl -> init('{comments}', '<a href="comments.php?what=art&id='.$id.'>skomentuj ['.$obj->art_comments.']</a>');
  25. $tpl -> init('{read}', '<a href="art.php?id='.$obj->art_id.'>czytaj</a>');
  26. $tpl -> run();
  27. }
  28.  
  29.  
  30. } else {
  31. return 'w tej katagorii nie wystepuja zadne artykuly';
  32. }
  33. }
  34.  
  35. // .....skrypt
  36.  
  37. ?>

i teraz gdy jest ta petla to nie wczytuje calego szablonu index.tpl do konca a jedynie sam początek i później art/art_in_cat.tpl i to już koniec wczytania szablonu, i tylko jeden wynik sie pokazuje zamiast wszystkich co są w kategorii. jak to naprawić bo niestety pierwszy to taki moj parser, zawsze sie bawilem ze dawalem kodzie thema <? newsy() ?> itp i bylo dobrze,
deirathe
hmm... a może by tak zrobić żeby init() pobierała $sSource i odrazu podmieniała dany ciąg znaków na zawartość zmiennej, a run tylko wyświetlała:
  1. <?php
  2. class Template{
  3.  
  4.  var $sSource;
  5.  var $sParsed;
  6.  var $aValues = array();
  7.  
  8.  function load($sTemplate) {
  9. return (!$this->sSource = @file_get_contents($sTemplate)) ? false : true;
  10.  }
  11.  
  12.  
  13.  function init($sKey, $sValue) {
  14.  
  15. $this->aValues[$sKey] = $sValue;
  16. $this->sSource = str_replace($sKey,$sValue,$this->sSource);
  17.  }
  18.  
  19.  function run() {
  20. print $this->sSource;
  21.  }
  22. } 
  23. ?>

tablice zostawiłem na wszelki wypadek tongue.gif
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.