00001 <?php
00002
00003 require_once 'Panda/View/Abstract.php';
00004
00023 class Panda_View_HTML
00024 extends Panda_View_Abstract
00025 {
00031 protected $document;
00032
00038 protected $template;
00039
00045 protected $partials;
00046
00052 public function setTemplate($template)
00053 {
00054 if (is_file($template)) {
00055 $this->template = $template;
00056 }
00057 }
00058
00065 public function setPartial($partial, $target)
00066 {
00067 if (is_file($partial) && !empty($target)) {
00068 $this->partials[$partial] = $target;
00069 }
00070 }
00071
00077 public function unsetPartial($partial)
00078 {
00079 if (array_key_exists($partial, $this->partials)) {
00080 unset($this->partials[$partial]);
00081 }
00082 }
00083
00090 protected function parse($file)
00091 {
00092 $out = '';
00093
00094 if (is_file($file)) {
00095 ob_start();
00096
00097 include $file;
00098 $out = ob_get_contents();
00099
00100 ob_end_clean();
00101 }
00102
00103 return $out;
00104 }
00105
00112 protected function load($source, $target)
00113 {
00114 $xpath = new DOMXPath($this->document);
00115 $items = $xpath->query($target);
00116
00117 if ($items->length > 0) {
00118 $source = $this->parse($source);
00119 $fragment = $this->document->createDocumentFragment();
00120
00121 $fragment->appendXML($source);
00122 $items->item(0)->appendChild($fragment);
00123 }
00124 }
00125
00130 public function render()
00131 {
00132 if (empty($this->template) || !is_array($this->partials)) {
00133 throw new Exception('Unable to render: incomplete view configuration.');
00134 }
00135
00136 $templateContents = $this->parse($this->template);
00137
00138 if (!empty($templateContents)) {
00139 $this->document = new DOMDocument;
00140 $this->document->loadHTML($templateContents);
00141
00142 foreach ($this->partials as $source => $target) {
00143 $this->load($source, $target);
00144 }
00145
00146 return $this->output($this->document->saveHTML());
00147 }
00148 }
00149 }