Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> System szablonów
thoriam
post
Post #1





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 23.02.2012

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


Witam mam mały problem z wybraniem szablonów. Korzystam od jakiegoś czasu ze smarty ale opinii dobrych o tym nie słyszałem. Zresztą samemu zobaczyłem że mielenie całej templatki nie jest chyba zbyt dobrym pomysłem jeśli chodzi o wydajność. Szukam czegoś innego. No właśnie czego (IMG:style_emoticons/default/questionmark.gif) Na pewno potrzebny mi jest cache i tyle. Co polecacie??
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
thoriam
post
Post #2





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 23.02.2012

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


tzn. ja już mam pewien bardzo prosty szablon dwie klasy podstawowa
  1.  
  2. abstract class View
  3. {
  4. private $_templateDir ='/';
  5. private $_extension ='php';
  6.  
  7. public function __construct()
  8. {
  9. $this->setTemplateDir($this->_templateDir);
  10. $this->setExtension($this->_extension);
  11.  
  12. }
  13.  
  14. public function setTemplateDir($dir)
  15. {
  16. $return = CUR_PATH .'/application/views/';
  17. if(strlen($dir) > 0 && $dir[strlen($dir) - 1] != '/')
  18. {
  19. $dir .= '/';
  20. }
  21. if(!is_dir($dir))
  22. {
  23. // throw new RuntimeException('Podany katalog szablonów '.$dir.' jest niedostępny.');
  24. }
  25.  
  26.  
  27. $return .=$dir;
  28. $this->_templateDir = $return;
  29.  
  30. }
  31.  
  32. public function getTemplateDir()
  33. {
  34. return $this->_templateDir;
  35. }
  36.  
  37. public function setExtension($extension)
  38. {
  39. if(!ctype_alnum($extension))
  40. {
  41. throw new DomainException('Nazwa rozszerzenia może zawierać wyłącznie litery i cyfry.');
  42. }
  43. $this->_extension = $extension;
  44. }
  45.  
  46. public function getExtension()
  47. {
  48. return $this->_extension;
  49. }
  50.  
  51. public static function factory($viewName)
  52. {
  53. return new EngineView($viewName);
  54. }
  55. }
  56.  


oraz ta wywołujaca sablon
  1.  
  2. class EngineView extends View
  3. {
  4. private $_template;
  5. private $_path;
  6. private $_data = array();
  7.  
  8.  
  9. public function __construct($template)
  10. {
  11. parent::__construct();
  12. $this->_template = $template;
  13. $path = $this->getTemplateDir();
  14. $path .=$template.'.'.$this->getExtension();
  15.  
  16. if(!file_exists($path))
  17. {
  18. throw new RuntimeException('Określony szablon '.$template.' nie istnieje.');
  19. }
  20.  
  21. $this->_path = $path;
  22. }
  23.  
  24. public function getTemplate()
  25. {
  26. return $this->_template;
  27. }
  28.  
  29. public function __set($name, $value)
  30. {
  31. $this->_data[$name] = $value;
  32. }
  33.  
  34. public function __get($name)
  35. {
  36. return $this->_data;
  37. }
  38.  
  39. public function render()
  40. {
  41. extract($this->_data);
  42.  
  43. require($this->_path);
  44. }
  45. }


bawiłem się trochę tym, ale nie chcę od nowa wynajdywać koła więc szukam może lepszego rozwiązania. No i średnio wiem jak zrobić ten cache. Może ktoś zarzuci jakiś przykład?
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 2.10.2025 - 19:06