Example #1
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);
 }
Example #2
0
 /**
  * Translates content of node, automatically replaces expressions with placeholders.
  * Expects content of node to be plural and macro parameter to be singular.
  * @param HtmlNode $node Node.
  * @param TemplateNode|null $value Macro parameter.
  */
 public function _tn(HtmlNode $node, TemplateNode $value)
 {
     $translate = '';
     $num = 1;
     $params = array();
     $before = array();
     foreach ($node->getChildren() as $child) {
         if ($child instanceof TextNode) {
             $translate .= $child->text;
         } else {
             if ($child instanceof PhpNode and !$child->statement) {
                 $translate .= '%' . $num;
                 $params[] = $child->code;
                 $num++;
             } else {
                 $translate .= '%' . $num;
                 $params[] = PhpNode::expr($child);
                 $num++;
             }
         }
     }
     if (count($params) == 0) {
         $params = '';
     } else {
         $params = ', ' . implode(', ', $params);
     }
     $translate = trim($translate);
     $node->clear();
     $phpNode = new PhpNode('tn(' . var_export($translate, true) . ', ' . PhpNode::expr($value)->code . $params . ')');
     $node->append($phpNode);
 }
Example #3
0
 /**
  * Convert HTML DOM to a template node.
  * @param \simple_html_dom_node $node DOM node.
  * @return TemplateNode Template node.
  */
 public function convert(\simple_html_dom_node $node)
 {
     if ($node->tag === 'text' or $node->tag === 'unknown') {
         return new TextNode($node->innertext);
     } else {
         if ($node->tag === 'comment') {
             if (preg_match('/^<!-- *\\{(.*)\\} *-->$/ms', $node->innertext, $matches) === 1) {
                 return new PhpNode($matches[1], true);
             }
             return new TextNode('');
         } else {
             $output = new HtmlNode($node->tag);
             foreach ($node->attr as $name => $value) {
                 if (preg_match('/^{(.*)}$/', $value, $matches) === 1) {
                     $value = new PhpNode($matches[1]);
                 } else {
                     if ($value !== true) {
                         $value = new TextNode($value);
                     } else {
                         $value = null;
                     }
                 }
                 if (strpos($name, ':') === false) {
                     $output->setAttribute($name, $value);
                 } else {
                     //           list($ns, $name) = explode(':', $name, 2);
                     if ($value === true) {
                         $value = null;
                     }
                     $output->addMacro($name, $value);
                 }
             }
             foreach ($node->nodes as $child) {
                 $output->append($this->convert($child));
             }
             return $output;
         }
     }
 }
Example #4
0
 public function _icon(HtmlNode $node, $value)
 {
     $icon = new HtmlNode('span');
     $icon->addClass('icon');
     $icon->append(new PhpNode('$Icon->icon("' . $value . '")'));
     $node->prepend($icon);
 }