Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> DOMDocument i getElementsByTagNameNS
deirathe
post 22.01.2008, 14:35:33
Post #1





Grupa: Zarejestrowani
Postów: 426
Pomógł: 32
Dołączył: 24.05.2007

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


index.php
  1. <?php
  2. class Template
  3. {
  4. private $DOMDoc;
  5. public $vars;
  6. public $templateDir;
  7. public $NS="http://someexaple.com";
  8. public function __construct($templateFile=null,$version=null,$encoding=null)
  9. {
  10. $this->DOMDoc = new DOMDocument($version,$encoding);
  11. if($templateFile)
  12. $this->setTemplate($templateFile);
  13.  
  14. }
  15. public function setTemplateDir($templateDir)
  16. {
  17. $this->templateDir = $templateDir;
  18. }
  19. public function setTemplate($templateFile)
  20. {
  21. $this->DOMDoc->load($this->templateDir.$templateFile);
  22. return true;
  23. }
  24. private function select($path=null)
  25. {
  26. if(!$path)
  27. throw new Exception();
  28. $type = substr($path,0,1);
  29. switch($type)
  30. {
  31. case "@":
  32. //petle
  33. break;
  34. case "#":
  35. //dokument->xpath
  36. break;
  37. case "/":
  38. $path = substr($path,2);
  39. $path = explode("/",$path);
  40. $input = $this->vars;
  41. foreach($path as $route)
  42. $input = $input[$route];
  43. break;
  44. default:
  45. throw new Exception("nie prawdilowae odniesienie do zmiennej:".$path." o typie:'".$type."'");
  46. break;
  47. }
  48. return $input;
  49.  
  50. }
  51. private function replaceNodeWithString($oldNode,$xmlString)
  52. {
  53. $f = $this->DOMDoc->createDocumentFragment();
  54. if($f->appendXML($xmlString))
  55. $oldNode->parentNode->replaceChild($f,$oldNode);
  56. }
  57. /*w przyszlosci doda podmienianie wsztsrkich tagow odnoszacych sie do tych samych
     plikow*/
  58. private function applyImports()
  59. {
  60. $elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "import");
  61. foreach($elements as $item)
  62. {
  63. $includePath="";
  64. if($item->hasAttribute("href"))
  65. $includePath = $item->getAttribute("href");
  66. elseif($item->hasAttribute("select"))
  67. $includePath = $this->select($item->getAttribute("select"));
  68. else
  69. throw new Exception();
  70. if($includePath)
  71. {
  72. if($text = file_get_contents($this->templateDir.$includePath))
  73. {
  74. $this->replaceNodeWithString($item,$text);
  75.  
  76. }
  77. else
  78. throw new Exception();
  79. }
  80. else
  81. throw new Exception();
  82.  
  83. echo "a";
  84.  
  85. }
  86. }
  87. private function applyValues()
  88. {
  89. $elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "value");
  90. foreach($elements as $item)
  91. {
  92. if(substr($item->getAttribute("select"),0,1)=="/")
  93. {
  94. $value = $this->select($item->getAttribute("select"));
  95. $this->replaceNodeWithString($item,$value);
  96. }
  97.  
  98. }
  99. }
  100. public function applyTemplates()
  101. {
  102. $this->applyImports();
  103. $this->applyValues();
  104. echo $this->DOMDoc->saveXML();
  105. }
  106.  
  107. }
  108. $tpl = new Template("tpl.tpl","1.0","utf-8");
  109. $tpl->vars = array("asd"=>"wartos dla zmiennej asd","as4"=>array("as2"=>"wartosc dla as4/as2"),"new"=>"plug.tpl");
  110. $tpl->applyTemplates();
  111. ?>

tpl.tpl
  1. <?xml version="1.0"?>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:tpl="http://someexaple.com" >
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title><tpl:import select="//new" /></title>
  6. </head>
  7. <body>
  8. <tpl:import select="//new" />
  9. <tpl:import select="//new" />
  10. <tpl:import select="//asd" />
  11. <tpl:import select="//new" />
  12. <tpl:value select="//asd" />
  13. <tpl:foreach select="//asd2">   sdfds
  14.      <tpl:foreach id="my" select="/aaa1" start="/zmienna" stop="10">
  15.          <tpl:lang href="</span>">
  16.       <tpl:value select="@my/keys/1"/>
  17.       </tpl:lang>
  18.      </tpl:foreach>
  19.      <tpl:import select="//new" />
  20. </tpl:foreach>
  21. <tpl:il8n select="//dict/asa"/>
  22. <tpl:value select="//as4/as2" /> <tpl:value select="/as4/as2" />
  23. <tpl:test not="value" equal="value,value" isset="value">
  24. <tpl:else not="" equal="" isset="</span>"></tpl:else>
  25. <tpl:else not="" equal="" isset="</span>"></tpl:else>
  26. </tpl:test>
  27. <tpl:switch select="</span>">
  28. <tpl:case value="">
  29. <tpl:import select="<<span style='color:blue'>span style='color:orange'>//new" />
  30. </tpl:case>
  31. </tpl:switch>
  32. <tpl:import href="plug.tpl" />
  33.  
  34. </body>
  35. </html>

plug.tpl
  1. <b>plugin lub cos</b>

Problem polega na tym że klasa mi nie chce podmieniać wszystkich includow z templaka, podmienia tylko niektore na co wskazuje literka a ktora powtarza sie tyle razy ile jest includow w pliku tpl.tpl,
natomiast gdy zrobie to tak:
  1. <?php
  2. $tpl = new DOMDocument("1.0");
  3. $tpl->load("tpl.tpl");
  4. $e = $tpl->getElementsByTagNameNS("http://someexaple.com","import");
  5. foreach($e as $i)
  6. echo "a";
  7. ?>

to wykrywa wszystkie includy. Co moze byc zgrane?
tutaj mozna znalezc wlaczona klase
Dzieki z gory za pomoc

Ten post edytował deirathe 22.01.2008, 14:49:53


--------------------
Kawałek mojego blogu
Everything should be as simple as possible but not simpler.
A Einstein
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: 19.07.2025 - 15:36