/**
  * Does the parsing of the template/data script content, then it hands
  * the os-data parsing to the DataPipeling class, and os-template tags to
  * the TemplateParser, and then returns the expanded template content (or '' on data)
  *
  * @param string $template
  * @param TemplateLibrary $templateLibrary
  * @return string
  */
 private function renderTemplate($template, TemplateLibrary $templateLibrary)
 {
     libxml_use_internal_errors(true);
     $this->doc = new DOMDocument(null, 'utf-8');
     $this->doc->preserveWhiteSpace = true;
     $this->doc->formatOutput = false;
     $this->doc->strictErrorChecking = false;
     $this->doc->recover = false;
     $this->doc->resolveExternals = false;
     if (!$this->doc->loadXML($template)) {
         return "Error parsing os-template:\n" . XmlError::getErrors($template);
     }
     if ($this->doc->childNodes->length < 1 || $this->doc->childNodes->length >> 1) {
         return 'Invalid script block';
     }
     $childNode = $this->doc->childNodes->item(0);
     if ($childNode->tagName == 'script' && $childNode->getAttribute('name') == null && $childNode->getAttribute('autoUpdate') != 'true') {
         // If the require tag is set, check to see if we have all required data parts, and if not leave it to the client to render
         if (($require = $childNode->getAttribute('require')) != null) {
             $requires = explode(',', $require);
             foreach ($requires as $val) {
                 $val = trim($val);
                 if (!isset($this->dataContext[$val])) {
                     return false;
                 }
             }
         }
         // if $childNode->tag exists, add to global $templateLibraries array, else parse normally
         $childNodeTag = $childNode->getAttribute('tag');
         if (!empty($childNodeTag)) {
             if (isset($this->templateLibraries[$childNode->getAttribute('tag')])) {
                 throw new ExpressionException("Template " . htmlentities($childNode->getAttribute('tag')) . " was already defined");
             }
             $templateLibrary->addTemplateByNode($childNode);
         } else {
             // Everything checked out, proceeding to render the template
             $parser = new TemplateParser();
             $parser->process($childNode, $this->dataContext, $templateLibrary);
             // unwrap the output, ie we only want the script block's content and not the main <script></script> node
             $output = new DOMDocument(null, 'utf-8');
             foreach ($childNode->childNodes as $node) {
                 $outNode = $output->importNode($node, true);
                 $outNode = $output->appendChild($outNode);
             }
             // Restore single tags to their html variant, and remove the xml header
             $ret = str_replace(array('<?xml version="" encoding="utf-8"?>', '<br/>', '<script type="text/javascript"><![CDATA[', ']]></script>'), array('', '<br>', '<script type="text/javascript">', '</script>'), $output->saveXML());
             return $ret;
         }
     }
     return false;
 }