Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klasa szablonów, templates
banpl
post
Post #1





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 2.11.2005
Skąd: Radomsko

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


witam wszystkich smile.gif

mam taki mały problem ;/ , napisalem sobie prosta klase do obslugi szablonow ktorej kod widzicie tutaj :
  1. <?php
  2. class template{
  3.  
  4. private $root;
  5. private $files = array();
  6. private $template = array();
  7.  
  8. function __construct($root){
  9. if(is_dir($root)){
  10. $this -> root = $root;
  11. }else{
  12. die('Template : zla sciezka do szablonu !');
  13. }
  14. } // end __construct($root);
  15.  
  16. function loadFileS($file){
  17. if(!is_array($file)){
  18. die('Template : funkcja loadFileS przyjmuje tylko tablice !');
  19. }else{
  20. foreach($file as $handle => $FILE){
  21. if(file_exists($this -> root.'/'.$FILE)){
  22. $this -> files[$handle] = file_get_contents($this -> root.'/'.$FILE);
  23. }else{
  24. die('Template : blad ladowania pliku '.$FILE.'!');
  25. }
  26. }
  27. }
  28. } // end loadFileS($file);
  29.  
  30. function loadFile($handle, $file){
  31. if(file_exists($this -> root.'/'.$file)){
  32. $this -> files[$handle] = file_get_contents($this -> root.'/'.$file);
  33. }else{
  34. die('Template : blad ladowania pliku !');
  35. }
  36. } // end loadFile($file);
  37.  
  38. function addVars($handle, $values){
  39. if(!is_array($values)){
  40. die('Templates : funkcja addVars przyjmuje tylko tablice !');
  41. }else{
  42. foreach($values as $tag => $value){
  43. $this -> template[$handle][$tag] = $value;
  44. }
  45. }
  46. } // end addVars($handle, $values);
  47.  
  48. function addVar($handle, $tag, $value){
  49. $this -> template[$handle][$tag] = $value;
  50. } // end addVars($tag, $value);
  51.  
  52. function pparse($handle){
  53. if(count($this -> template[$handle]) > 0) {
  54. $keys = array_keys($this -> template[$handle]);
  55.  
  56. foreach ($keys as $key) {
  57. $this -> files[$handle] = str_replace($key, $this -> template[$handle][$key], $this -> files[$handle]);
  58. }
  59.  
  60. }
  61. return $this -> files[$handle];
  62. } // end parse();
  63.  
  64. function parse($handle){
  65. print ($this -> pparse($handle));
  66. } // end parse($handle);
  67.  
  68. }
  69. ?>


natomiast tak wyglada zastosowanie tego w praktyce :
  1. <?php
  2. include ("tpl.class.php");
  3.  
  4. $tpl = new template("tpl");
  5.  
  6. $tpl -> loadFileS(array(
  7. 'index' => 'index.tpl',
  8. 'heder' => 'heder.tpl',
  9. 'bottom' => 'bottom.tpl'
  10. ));
  11.  
  12. $tpl -> addVars('heder', array(
  13. '{TITLE}' => 'Spokojnie, to tylko test!',
  14. '{META}' => '',
  15. '{BGCOLOR}' => '#f0f0f0',
  16. '{KODOWANIE}' => 'iso-8859-2'
  17. ));
  18.  
  19. $tpl -> addVars('bottom', array(
  20. '{TEXT}' => 'jakis tam sobie text',
  21. '{TEXT_DWA}' => 'kolejny bzdurny text biggrin.gif'
  22. ));
  23.  
  24. $tpl -> addVars('index', array(
  25. '{HEDER}' => $tpl -> pparse('heder'),
  26. '{TRESC}' => 'Tutaj powinna znajdować się właœciwa treœć strony :)',
  27. '{BOTTOM}' => $tpl -> pparse('bottom')
  28. ));
  29.  
  30. $tpl -> parse('index');
  31. ?>


i niby wszytsko jest ok, ale ja bym chcial zrobic taki bajer ze gry zamieniam jakis znacznik z szablonu dajmy na to {TEXT} chcialbym uzyc '{TEXT}' => require('plik.php') no i wlasnie tu jest problem bo zamiast ladnie podminic sformulowanie {TEXT} i wrzucic plik w to miejsce co trzeba to tak nie jest, plik jest includowany od tak sobei na poczatku strony

czy jest jakis sposob zeby cos zty m zrobic questionmark.gif


--------------------
Warsztat :: Windows7 / Ubuntu | PHP 5 | MySQL 5 | Eclipse / NetBeans | Opera / Firefox
Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 21.08.2025 - 03:40