protected function processTemplateBlocks(Template $template, &$globals, RendererInterface $renderer)
 {
     if ($this->benchmarkRendering) {
         $this->Benchmark->start('process-template-blocks-' . $template->getName());
     }
     $templateBlocks = $template->getTemplateBlocks();
     $data = $template->getData();
     $locals = $template->getLocals();
     $handler = $template->getContentType() != 'html' && $template->getContentType() != '' ? $template->getContentType() . '-' : '';
     $renderout = '';
     $contents = '';
     $parsedContents = '';
     $locals['DisplayRecords'] = sizeof($data);
     $locals['Count'] = 1;
     if (!empty($data)) {
         //if(LOG_ENABLE) System::log(self::$logType, 'Parsing contents block');
         if (!isset($templateBlocks[$handler . 'contents']) && !isset($templateBlocks[$handler . 'header']) && !isset($templateBlocks[$handler . 'exec'])) {
             throw new Exception('Template [' . $template->getName() . '] is missing a template block for [' . $handler . 'contents] or [' . $handler . 'header] or [' . $handler . 'exec]');
         }
         $eBlock = isset($templateBlocks[$handler . 'exec']) ? $templateBlocks[$handler . 'exec'] : '';
         $cBlock = isset($templateBlocks[$handler . 'contents']) ? $templateBlocks[$handler . 'contents'] : '';
         $ciBlock = isset($templateBlocks[$handler . 'contents-inbetween']) ? $templateBlocks[$handler . 'contents-inbetween'] : '';
         if (!empty($eBlock)) {
             $contents = $eBlock;
             $contents = $this->parseSetters($contents, $locals);
             $contents = $this->parseConditions($contents, $locals);
             $contents = $this->parseFilters($contents, $locals);
             $contents = $this->parseAssets($contents, $locals);
             // parse dependent sub modules
             $template->setLocals($locals);
             // $this->Logger->debug("Parsing dependent includes for [{$template->getName()}]...");
             $contents = $this->parseTemplateIncludes($contents, $template->getContentType(), $template, $globals, $renderer, false, true);
             $contents = $this->parseFormatVariables($contents, $locals);
             $renderout .= $contents;
         } else {
             // List Processing of the Data
             foreach ((array) $data as $row) {
                 $contents = $cBlock;
                 // if(strpos($contents, '{% set ') !== FALSE) {
                 //     while(preg_match("/(.*?)\{\%\s+set\s+([^\%]+?)\s+\%\}\s*(.*?)\s?\{\%\s+endset\s+\%\}\s*(.*)/s",$contents,$m)) {
                 //         if(!array_key_exists($m[2], $row)) {
                 //             $val = $this->parseFormatVariablesAndFilters($m[3], $row);
                 //             $row[$m[2]] = $val;
                 //         }
                 //         $contents = $m[1]. $m[4];
                 //      }
                 // }
                 if ($locals['DisplayRecords'] != $locals['Count']) {
                     $contents .= $ciBlock;
                 }
                 if (!is_array($row)) {
                     if ($row instanceof Node) {
                         /*
                          * Populating Node itself into the row so it can be used in templates,
                          * passed to events, filters, etc.  see ticket #30
                          * todo: investigate populating 'Node' in populateNodeCheaters directly
                          */
                         $node = $row;
                         $row = $this->NodeMapper->populateNodeCheaters($row)->toArray();
                         $row['Node'] = $node;
                     } else {
                         if ($row instanceof Object) {
                             $row = $row->toArray();
                         } else {
                             throw new Exception("data is not an array\n" . print_r($row, true));
                         }
                     }
                 }
                 $row_locals = array_merge($locals, $row);
                 $row_locals['SerializedData'] = $row;
                 //if(LOG_ENABLE) System::log(self::$logType, 'Locals ['.print_r($locals, true).']');
                 $contents = $this->parseSetters($contents, $row_locals);
                 $contents = $this->parseConditions($contents, $row_locals);
                 $contents = $this->parseFilters($contents, $row_locals);
                 $contents = $this->parseAssets($contents, $row_locals);
                 // parse dependent sub modules
                 $template->setLocals($row_locals);
                 // $this->Logger->debug("Parsing dependent includes for [{$template->getName()}]...");
                 $contents = $this->parseTemplateIncludes($contents, $template->getContentType(), $template, $globals, $renderer, false, true);
                 $contents = $this->parseFormatVariables($contents, $row_locals);
                 $locals['Count']++;
                 $parsedContents .= $contents;
             }
             $locals = $row_locals;
             $template->setLocals($locals);
             // headers and footers can use format variables from the final row
             $renderout = '';
             if (!empty($templateBlocks[$handler . 'header'])) {
                 $header = $templateBlocks[$handler . 'header'];
                 $header = $this->parseSetters($header, $locals);
                 $header = $this->parseConditions($header, $locals);
                 $header = $this->parseFilters($header, $locals);
                 $header = $this->parseAssets($header, $locals);
                 $template->setLocals($locals);
                 // parse dependent sub modules
                 $header = $this->parseTemplateIncludes($header, $template->getContentType(), $template, $globals, $renderer, false, true);
                 $header = $this->parseFormatVariables($header, $locals);
                 $renderout .= $header;
             }
             $renderout .= $parsedContents;
             if (!empty($templateBlocks[$handler . 'footer'])) {
                 $footer = $templateBlocks[$handler . 'footer'];
                 $footer = $this->parseSetters($footer, $locals);
                 $footer = $this->parseConditions($footer, $locals);
                 $footer = $this->parseFilters($footer, $locals);
                 $footer = $this->parseAssets($footer, $locals);
                 $template->setLocals($locals);
                 // parse dependent sub modules
                 $footer = $this->parseTemplateIncludes($footer, $template->getContentType(), $template, $globals, $renderer, false, true);
                 $footer = $this->parseFormatVariables($footer, $locals);
                 $renderout .= $footer;
             }
         }
     } else {
         if (!$template->isTopTemplate() && !array_key_exists($handler . 'contents', $templateBlocks) && !array_key_exists($handler . 'noresults', $templateBlocks)) {
             throw new Exception('Template [' . $template->getName() . '] is missing a template block for [' . $handler . 'noresults] or [' . $handler . 'contents] or [' . $handler . 'exec]');
         }
         if (array_key_exists($handler . 'noresults', $templateBlocks)) {
             $renderout = $templateBlocks[$handler . 'noresults'];
         } elseif ($template->getData() === null && empty($locals['DataSource']) && array_key_exists($handler . 'exec', $templateBlocks)) {
             $renderout = $templateBlocks[$handler . 'exec'];
         } elseif ($template->getData() === null && empty($locals['DataSource']) && array_key_exists($handler . 'contents', $templateBlocks)) {
             $renderout = $templateBlocks[$handler . 'contents'];
         } elseif ($template->isTopTemplate()) {
             throw new NotFoundException($template->getName());
         }
         $renderout = $this->parseSetters($renderout, $locals);
         $renderout = $this->parseConditions($renderout, $locals);
         $renderout = $this->parseFilters($renderout, $locals);
         $renderout = $this->parseConditions($renderout, $locals);
         $template->setLocals($locals);
         // parse dependent sub modules
         //$template->setLocals($locals);
         $renderout = $this->parseTemplateIncludes($renderout, $template->getContentType(), $template, $globals, $renderer, false, true);
         $renderout = $this->parseFormatVariables($renderout, $locals);
     }
     //if(LOG_ENABLE) System::log(self::$logType, 'Render out ['.print_r($renderout, true).']');
     if ($this->benchmarkRendering) {
         $this->Benchmark->end('process-template-blocks-' . $template->getName());
     }
     return $renderout;
 }