Beispiel #1
0
 /**
  * Compile a template file by reading it, converting the DOM using
  * {@see convert()}, then applying macros using {@see transform()}.
  * @param string $template Template file path.
  * @return string PHP template content. 
  * @throws InvalidTemplateException If template is inaccessible or invalid.
  */
 public function compile($template)
 {
     $dom = new \simple_html_dom();
     $this->currentTemplate = $template;
     $file = file_get_contents($template);
     if ($file === false) {
         throw new InvalidTemplateException(tr('Could not read template: %1', $template));
     }
     if (!$dom->load($file, true, false)) {
         throw new InvalidTemplateException(tr('Could not parse template: %1', $template));
     }
     $root = new InternalNode();
     $main = $dom->find('[j:main]', 0);
     if (isset($main)) {
         $root->append($this->convert($main));
     } else {
         foreach ($dom->find('*, text') as $html) {
             if ($html->parent->tag != 'root') {
                 continue;
             }
             $root->append($this->convert($html));
         }
     }
     $this->transform($root);
     return $root->__toString();
 }
Beispiel #2
0
 public function _field(HtmlNode $node, TemplateNode $value)
 {
     $internal = new InternalNode();
     foreach ($node->getChildren() as $child) {
         $internal->append($child->detach());
     }
     $start = '$field = $Form->field(' . PhpNode::expr($value)->code . ', ';
     $start .= PhpNode::attributes($node)->code . ')';
     $internal->prepend(new PhpNode($start, true));
     $internal->append(new PhpNode('$field->end()'));
     $node->replaceWith($internal);
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 public function __toString()
 {
     $output = '<' . $this->tag;
     foreach ($this->attributes as $name => $value) {
         $output .= ' ' . $name;
         if (isset($value) and !$value->isNull()) {
             $output .= '="' . $value . '"';
         }
     }
     if (count($this->content) == 0 and $this->selfClosing) {
         return $output . ' />';
     }
     $output .= '>';
     $output .= parent::__toString();
     $output .= '</' . $this->tag . '>';
     return $output;
 }