/** * 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(); } }