1. <?php
  2. class Template
  3. {
  4. private $DOMDoc;
  5. public $vars;
  6. public $templateDir;
  7. public $NS="http://someexaple.com";
  8. private $loops;
  9. public function __construct($templateFile=null,$version=null,$encoding=null)
  10. {
  11. $this->DOMDoc = new DOMDocument();
  12. if($templateFile)
  13. $this->setTemplate($templateFile);
  14.  
  15. }
  16. public function setTemplateDir($templateDir)
  17. {
  18. $this->templateDir = $templateDir;
  19. }
  20. public function setTemplate($templateFile)
  21. {
  22. $this->DOMDoc->load($this->templateDir.$templateFile);
  23. return true;
  24. }
  25. private function select($path=null,$default=null)
  26. {
  27. $type = substr($path,0,1);
  28. switch($type)
  29. {
  30. case "@":
  31. //petle
  32. break;
  33. case "#":
  34. //dokument->xpath
  35. break;
  36. case "/":
  37.  
  38. $path = substr($path,1);
  39. $path =preg_replace("#/(w+)#is","['$1']",$path);//tworzymy sciezke do zmiennej
  40. $path="$input = $this->vars".$path.";";
  41. eval($path);
  42. break;
  43. default:
  44. if($default=="/end/")
  45. $input = $path;
  46. break;
  47. }
  48. if(trim($input))
  49. return $input;
  50. elseif($default)
  51. return $this->select($default,'/end/');
  52.  
  53. }
  54. private function replaceNodeWithString($oldNode,$xmlString)
  55. {
  56. $f = $this->DOMDoc->createDocumentFragment();
  57. if($f->appendXML($xmlString.' '))//szybki hak na puste stringi :)
  58. $oldNode->parentNode->replaceChild($f,$oldNode);
  59. }
  60. /*w przyszlosci doda podmienianie wsztsrkich tagow odnoszacych sie do tych samych
     plikow*/
  61. private function applyImports()
  62. {
  63. $e = $this->DOMDoc->getElementsByTagNameNS($this->NS, "import");
  64. $i=0;
  65. foreach($e as $item)
  66. {
  67.  
  68.  
  69. $includePath="";
  70. if($item->hasAttribute("href"))
  71. $includePath = $item->getAttribute("href");
  72. elseif($item->hasAttribute("select"))
  73. $includePath = $this->select($item->getAttribute("select"));
  74. else
  75. throw new Exception();
  76. if($includePath)
  77. {
  78. //echo $includePath;
  79. if($data = @file_get_contents($this->templateDir.$includePath))
  80. {
  81. $toProgress[$i]['item']=$item;
  82. $toProgress[$i]['data']= '<includedTPL xmlns:tpl="'.$this->NS.'">'.$data.'</includedTPL>';//tag ktory dodaje ns dla includa, ktory go nie ma
  83. }
  84. else
  85. throw new Exception();
  86. }
  87. else
  88. {
  89. throw new Exception();
  90. }
  91. $i++;
  92.  
  93. }
  94. if($toProgress){
  95. foreach($toProgress as $p)
  96. $this->replaceNodeWithString($p['item'],$p['data']);
  97. }
  98. //echo "znalazł imprtow:".$i;
  99. }
  100. private function applyValues()
  101. {
  102. $elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "value");
  103. $i=0;
  104. foreach($elements as $item)
  105. {
  106. if(substr($item->getAttribute("select"),0,1)!="@")
  107. {
  108. $value = $this->select($item->getAttribute("select"),$item->getAttribute("default"));
  109. $toProgress[$i]['item']=$item;
  110. $toProgress[$i]['data']=$value;
  111. }
  112. $i++;
  113.  
  114. }
  115.  
  116. foreach($toProgress as $p)
  117. $this->replaceNodeWithString($p['item'],$p['data']);
  118. }
  119. private function applyLoops()
  120. {
  121. $elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "loop");
  122. foreach($elements as $item)
  123. {
  124. if($name = $item->getAttribute("name"))
  125. {
  126. if($item->getAttribute("select") or $item->getAttribute("default"))
  127. {
  128. $value = $this->select($item->getAttribute("select"),$item->getAttribute("default"));
  129. $uStart = (int)$item->getAttribute("start");
  130. $uStart?$start=$uStart:$start=0;
  131. $uMax = (int) $item->getAttribute("max");
  132. $uMax?$max=$uMax:$max=count($value);
  133. $uStep = (int) $item->getAttribute("step");
  134. $uStep?$step=$uStep:$step=1;
  135. //ponizej utknalem i nie wiem jak to pociagnac
  136. for($i=$start;$i<=$max;$i+=$step)
  137. {
  138. echo $value[$i],"<br/>";
  139. }
  140.  
  141. }
  142. else
  143. throw new Exception();
  144. }
  145. else
  146. throw new Exception();
  147. }
  148. }
  149. private function removeNS($tpl)
  150. {
  151. return str_replace(array("<includedTPL>","</includedTPL>"),"",$tpl);
  152. }
  153. private function applyTests()
  154. {
  155. $elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "test");
  156. }
  157. public function applyTemplates()
  158. {
  159. $this->applyImports();
  160. $this->applyValues();
  161. $this->applyLoops();
  162. echo $this->removeNS($this->DOMDoc->saveXML());
  163. }
  164.  
  165. }
  166. class obj
  167. {
  168. public $my = "moja wartosc obiektu";
  169. }
  170.  
  171.  
  172.  
  173.  
  174. $tpl = new Template("tpl.tpl","1.0","utf-8");
  175. $tpl->vars = array("new"=>"Asasasasasaa new","ar"=>$ar);
  176. $tpl->applyTemplates();
  177. ?>

szablon
  1. <?xml version="1.0"?>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:tpl="http://someexaple.com" >
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. </head>
  5.  
  6. <tpl:value select="//new" default="#cos"/>
  7. <div id="cos">as dasd asds sd</div>
  8. <tpl:loop name="my" select=".a" default="//ar" start="2">
  9. <tpl:value select="//new" default="#cos"/>
  10. <div style="color:#FF0000">data:<tpl:value select="@my/index"/></div>
  11. <tpl:value select="@my/ar"/>
  12. </tpl:loop>
  13.  
  14.  
  15. </body>
  16. </html>


W pliku php zaznaczylem w ktorym miejscu utknalem, chcialem zrobic petle w taki sposob zeby w srodku niej mozna sie bylo odwolac do nadanej wczesniej tablicy "ar" poprzez <tpl:value select="@my/ar"/> i zeby reszta dokumentu ktory jest we wnatrz nie znikala. Ma ktos jakis pomysl, juz mecze to chyba z tydzien i nic sad.gif
klasa oczywiscie jesze nie jest skonczona wiec zmienne nadaje sie narazie tak jak sie nadaje pozniej to zrobie na przeladowaniach