/** * Render the child and its child nodes * * @param boolean $ret * @param int $depth * @param string $indent * @param string $errorIndent * @return mixed */ public function render($ret = false, $depth = 0, $indent = null, $errorIndent = null) { $datalist = parent::render(true, $depth, $indent) . $this->datalist->render(true, $depth, $indent); // Return or print the rendered child node output. if ($ret) { return $datalist; } else { echo $datalist; } }
/** * Render the nav object * * @param boolean $ret * @return mixed */ public function render($ret = false) { if (null === $this->nav) { $this->nav = $this->traverse($this->tree); } if ($ret) { return $this->nav->hasChildren() ? $this->nav->render($ret) : ''; } else { echo $this->nav->hasChildren() ? $this->nav->render($ret) : ''; } }
/** * Render the child and its child nodes * * @param boolean $ret * @param int $depth * @param string $indent * @param string $errorIndent * @return string */ public function render($ret = false, $depth = 0, $indent = null, $errorIndent = null) { $output = parent::render(true, $depth, $indent); $errors = null; $container = $this->errorDisplay['container']; $attribs = null; foreach ($this->errorDisplay['attributes'] as $a => $v) { $attribs .= ' ' . $a . '="' . $v . '"'; } // Add error messages if there are any. if (count($this->errors) > 0) { foreach ($this->errors as $msg) { if ($this->errorDisplay['pre']) { $errors .= "{$indent}{$this->indent}<" . $container . $attribs . ">{$msg}</" . $container . ">\n{$errorIndent}"; } else { $errors .= "{$errorIndent}{$indent}{$this->indent}<" . $container . $attribs . ">{$msg}</" . $container . ">\n"; } } } $this->output = $this->errorDisplay['pre'] ? $errors . $output : $output . $errors; if ($ret) { return $this->output; } else { echo $this->output; } }
<?php require_once '../../bootstrap.php'; use Pop\Dom\Child; try { $children = array(array('nodeName' => 'h1', 'nodeValue' => 'This is a header', 'attributes' => array('class' => 'headerClass', 'style' => 'font-size: 3.0em;'), 'childrenFirst' => false, 'childNodes' => null), array('nodeName' => 'div', 'nodeValue' => 'This is a div element', 'attributes' => array('id' => 'contentDiv'), 'childrenFirst' => false, 'childNodes' => array(array('nodeName' => 'p', 'nodeValue' => 'This is a paragraph1', 'attributes' => array('style' => 'font-size: 0.9em;'), 'childrenFirst' => false, 'childNodes' => array(array('nodeName' => 'strong', 'nodeValue' => 'This is bold!', 'attributes' => array('style' => 'font-size: 1.2em;')))), array('nodeName' => 'p', 'nodeValue' => 'This is another paragraph!', 'attributes' => array('style' => 'font-size: 0.9em;'))))); $parent = new Child('div'); $parent->setIndent(' ')->addChildren($children); $parent->render(); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
/** * Render view template file * * @return void */ protected function renderTemplate() { if (null !== $this->form) { // Get names and labels $children = $this->form->getChildren(); $template = $this->template; // Loop through the child elements of the form. foreach ($children as $child) { // Clear the password field from display. if ($child->getAttribute('type') == 'password') { $child->setValue(null); $child->setAttribute('value', null); } // Get the element name. if ($child->getNodeName() == 'fieldset') { $chdrn = $child->getChildren(); $attribs = $chdrn[0]->getAttributes(); } else { $attribs = $child->getAttributes(); } $name = isset($attribs['name']) ? $attribs['name'] : ''; $name = str_replace('[]', '', $name); // Set the element's label, if applicable. if (null !== $child->getLabel()) { // Format the label name. $label = new Child('label', $child->getLabel()); $label->setAttribute('for', $name); $labelAttributes = $child->getLabelAttributes(); if (null !== $labelAttributes) { foreach ($labelAttributes as $a => $v) { $label->setAttribute($a, $v); } } else { if ($child->isRequired()) { $label->setAttribute('class', 'required'); } } // Swap the element's label placeholder with the rendered label element. $labelReplace = $label->render(true); $labelReplace = substr($labelReplace, 0, -1); ${$name . '_label'} = $labelReplace; } // Calculate the element's indentation. $childIndent = substr($template, 0, strpos($template, '[{' . $name . '}]')); $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1); // Some whitespace clean up $length = strlen($childIndent); $last = 0; $matches = []; preg_match_all('/[^\\s]/', $childIndent, $matches, PREG_OFFSET_CAPTURE); if (isset($matches[0])) { foreach ($matches[0] as $str) { $childIndent = str_replace($str[0], null, $childIndent); $last = $str[1]; } } // Final whitespace clean up $childIndent = substr($childIndent, 0, 0 - abs($length - $last)); // Set each child element's indentation. $childChildren = $child->getChildren(); $child->removeChildren(); foreach ($childChildren as $cChild) { $cChild->setIndent($childIndent . ' '); $child->addChild($cChild); } // Swap the element's placeholder with the rendered element. $elementReplace = $child->render(true, 0, null, $childIndent); $elementReplace = substr($elementReplace, 0, -1); $elementReplace = str_replace('</select>', $childIndent . '</select>', $elementReplace); $elementReplace = str_replace('</fieldset>', $childIndent . '</fieldset>', $elementReplace); ${$name} = $elementReplace; } $action = $this->form->getAttribute('action'); $method = $this->form->getAttribute('method'); $form = $this->form; } ob_start(); include $this->template; $this->output = ob_get_clean(); }
/** * Method to render the form using the template * * @return void */ protected function renderWithTemplate() { // Initialize properties and variables. $isFile = !(stripos($this->template, '.phtml') === false && stripos($this->template, '.php') === false); $template = $this->template; $fileContents = $isFile ? file_get_contents($this->template) : null; $this->output = null; $children = $this->form->getChildren(); // Loop through the child elements of the form. foreach ($children as $child) { // Clear the password field from display. if ($child->getAttribute('type') == 'password') { $child->setValue(null); $child->setAttributes('value', null); } // Get the element name. if ($child->getNodeName() == 'fieldset') { $chdrn = $child->getChildren(); $attribs = $chdrn[0]->getAttributes(); } else { $attribs = $child->getAttributes(); } $name = isset($attribs['name']) ? $attribs['name'] : ''; $name = str_replace('[]', '', $name); // Set the element's label, if applicable. if (null !== $child->getLabel()) { // Format the label name. $label = new Child('label', $child->getLabel()); $label->setAttributes('for', $name); $labelAttributes = $child->getLabelAttributes(); if (null !== $labelAttributes) { foreach ($labelAttributes as $a => $v) { $label->setAttributes($a, $v); } } else { if ($child->isRequired()) { $label->setAttributes('class', 'required'); } } // Swap the element's label placeholder with the rendered label element. $labelSearch = '[{' . $name . '_label}]'; $labelReplace = $label->render(true); $labelReplace = substr($labelReplace, 0, -1); $template = str_replace($labelSearch, $labelReplace, $template); ${$name . '_label'} = $labelReplace; } // Calculate the element's indentation. if (null === $fileContents) { $childIndent = substr($template, 0, strpos($template, '[{' . $name . '}]')); $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1); } else { $childIndent = substr($fileContents, 0, strpos($fileContents, '$' . $name)); $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1); } // Some whitespace clean up $length = strlen($childIndent); $last = 0; $matches = array(); preg_match_all('/[^\\s]/', $childIndent, $matches, PREG_OFFSET_CAPTURE); if (isset($matches[0])) { foreach ($matches[0] as $str) { $childIndent = str_replace($str[0], null, $childIndent); $last = $str[1]; } } // Final whitespace clean up if (null !== $fileContents) { $childIndent = substr($childIndent, 0, 0 - abs($length - $last)); } // Set each child element's indentation. $childChildren = $child->getChildren(); $child->removeChildren(); foreach ($childChildren as $cChild) { $cChild->setIndent($childIndent . ' '); $child->addChild($cChild); } // Swap the element's placeholder with the rendered element. $elementSearch = '[{' . $name . '}]'; $elementReplace = $child->render(true, 0, null, $childIndent); $elementReplace = substr($elementReplace, 0, -1); $elementReplace = str_replace('</select>', $childIndent . '</select>', $elementReplace); $elementReplace = str_replace('</fieldset>', $childIndent . '</fieldset>', $elementReplace); $template = str_replace($elementSearch, $elementReplace, $template); ${$name} = $elementReplace; } // Set the rendered form content and remove the children. if (!$isFile) { $this->form->setNodeValue("\n" . $template . "\n" . $this->form->getIndent()); $this->form->removeChildren(); $this->output = $this->form->render(true); } else { $action = $this->action; $method = $this->method; ob_start(); include $this->template; $this->output = ob_get_clean(); } }