Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Klasa do obsługi szablonów, Proszę o opinie ;)
Lirdoner
post 17.01.2012, 20:55:20
Post #1





Grupa: Zarejestrowani
Postów: 500
Pomógł: 1
Dołączył: 29.09.2009

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


Witam, więc napisałem swoją pierwszą klasę do obsługi szablonów
Wiem, że nie warto bi istnieje smarty czy inne tego typu systemy ale napisałem ją dla własnego doświadczenie i teraz się nią dziele prosząc jednocześnie o opinie wink.gif
  1. <?php
  2. class template {
  3. private $compileDir;
  4. private $templateDir;
  5. private $variables = Array();
  6.  
  7. public function setCompileDir($dir) {
  8. $src = substr($dir, -1);
  9. $src == '/' ? $this->compileDir = $dir : $this->compileDir = $dir.'/';
  10. }
  11.  
  12. public function setTemplateDir($dir) {
  13. $src = substr($dir, -1);
  14. $src == '/' ? $this->templateDir = $dir : $this->templateDir = $dir.'/';
  15. }
  16.  
  17. public function assign($name, $value) {
  18. $this->variables[$name] = $value;
  19. }
  20.  
  21. public function assigns($value) {
  22. if(is_array($value)) {
  23. foreach($value as $key => $result) {
  24. self::assign($key, $result);
  25. }
  26. }
  27. }
  28.  
  29. private function includeFile($file) {
  30. self::display($file);
  31. }
  32.  
  33. private function compile($tpl_file) {
  34. $content = file_get_contents($this->templateDir.$tpl_file);
  35. if(!empty($content)) {
  36. $pattern = array();
  37. $pattern[0] = '#{include file="(.*)"}#';
  38. $pattern[1] = '#{if \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
  39. $pattern[2] = '#{if \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
  40. $pattern[3] = '#{if !\$(.*)}#';
  41. $pattern[4] = '#{if \$(.*)}#';
  42. $pattern[5] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
  43. $pattern[6] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
  44. $pattern[7] = '#{elseif !\$(.*)}#';
  45. $pattern[8] = '#{elseif \$(.*)}#';
  46. $pattern[9] = '#{else}#';
  47. $pattern[10] = '#{/if}#';
  48. $pattern[11] = '#{\$(.*)\.(.*)}#U';
  49. $pattern[12] = '#{foreach \$(.*) as (.*)}#';
  50. $pattern[13] = "#{/foreach}#";
  51. $pattern[14] = '#{\$(.*)}#U';
  52.  
  53. $replacement = array();
  54. $replacement[0] = "<?php self::includeFile('$1') ?>";
  55. $replacement[1] = '<?php if($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
  56. $replacement[2] = '<?php if($this->variables[\'$1\'] $2 $3) { ?>';
  57. $replacement[3] = '<?php if(!$this->variables[\'$1\']) { ?>';
  58. $replacement[4] = '<?php if($this->variables[\'$1\']) { ?>';
  59. $replacement[5] = '<?php } elseif($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
  60. $replacement[6] = '<?php } elseif($this->variables[\'$1\'] $2 $3) { ?>';
  61. $replacement[7] = '<?php } elseif(!$this->variables[\'$1\']) { ?>';
  62. $replacement[8] = '<?php } elseif($this->variables[\'$1\']) { ?>';
  63. $replacement[9] = '<?php } else { ?>';
  64. $replacement[10] = '<?php } ?>';
  65. $replacement[11] = '<?php echo $$1[\'$2\'] ?>';
  66. $replacement[12] = '<?php foreach($this->variables[\'$1\'] as $2) { ?>';
  67. $replacement[13] = "<?php } ?>";
  68. $replacement[14] = '<?php echo $this->variables[\'$1\'] ?>';
  69.  
  70. $content = preg_replace($pattern, $replacement, $content);
  71.  
  72. $name = explode(".",$tpl_file);
  73. $header = '
  74. <?php
  75. $MD5_FILE_'.$name[0].' = "'.md5_file($this->templateDir.$tpl_file).'";
  76. ?>
  77. ';
  78. file_put_contents($this->compileDir.'template.'.$tpl_file.'.php',$header.$content);
  79. require_once($this->compileDir.'template.'.$tpl_file.'.php');
  80. } else {
  81. throw new Exception('Cannot get information from tpl file');
  82. }
  83. }
  84.  
  85. public function display($tpl_file) {
  86. if(!file_exists($this->templateDir.$tpl_file)) {
  87. throw new Exception('Template file does not found in '.$this->templateDir.$tpl_file);
  88. }
  89.  
  90. if(file_exists($this->compileDir.'template.'.$tpl_file.'.php')) {
  91. require_once($this->compileDir.'template.'.$tpl_file.'.php');
  92. $name = explode(".",$tpl_file);
  93. $name = 'MD5_FILE_'.$name[0];
  94. if($$name != md5_file($this->templateDir.$tpl_file)) {
  95. @unlink($this->compileDir.'template.'.$tpl_file.'.php');
  96. self::compile($tpl_file);
  97. }
  98. } else {
  99. self::compile($tpl_file);
  100. }
  101. }
  102.  
  103. }
  104. ?>

Obsługiwane tagi:
{if warunek}
{elseif warunek2}
{/if}
{foreach dane}
{/foreach}
{$zmienna.indeks} - tablica
{$zmienna} - zmienna
{include file="nazwapliku"} - dołącza plik do szablonu
Go to the top of the page
+Quote Post
CuteOne
post 26.01.2012, 22:26:45
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


Może i przydatne ale brakuje jednej ważnej rzeczy - obsługi wyjątków gdy zrobimy np. coś takiego

Kod
{foreach $a as $c}
fekfo
{/if}

//lub brakuje $

{foreach xxx as yyy}


Przykłady, można mnożyć w nieskończoność
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: 26.04.2024 - 02:14