public function loadTemplateExtended(Template $template, &$globals)
 {
     if ($this->benchmarkRendering) {
         $this->Benchmark->start('load-template-extended-' . $template->getName());
     }
     if (!$template->hasFile() || !$template->hasSetMatches() || !$template->hasContents()) {
         if (!($templateFileCached = $this->TemplateCache->get('t:' . $template->getName()))) {
             if (!$this->TemplateService->fileExists($template->getName())) {
                 throw new TemplateEngineException("Template file not found for template name: " . $template->getName());
             }
             $file = $this->TemplateService->resolveFile($template->getName());
             $template->setFile($file->getLocalPath());
             $this->Logger->debug("Loading template file: " . $template->getFile());
             $this->loadTemplateContents($template, $globals);
             $this->Logger->debug('Parsing set blocks: ' . $template->getName());
             $head = $template->getContents();
             if (($blockpos = strpos($head, '{% begin')) !== FALSE) {
                 $head = substr($head, 0, $blockpos);
             }
             if (preg_match_all("/\\{\\%\\s+(set|setGlobal|appendGlobal)\\s+([^\\%]+?)\\s+\\%\\}[\n\r\t]*(.*?)[\n\r\t]*\\{\\%\\s+end\\s+\\%\\}/s", $head, $setMatches, PREG_SET_ORDER)) {
                 $template->setSetMatches($setMatches);
             } else {
                 $template->setSetMatches(array());
             }
             $this->TemplateCache->put('t:' . $template->getName(), $template, 0);
         } else {
             $template->setFile($templateFileCached->getFile());
             $template->setSetMatches($templateFileCached->getSetMatches());
             $template->setContents($templateFileCached->getContents());
         }
     }
     $params = $template->getLocals();
     $templateSetGlobals = array();
     foreach ($template->getSetMatches() as $m) {
         $m[3] = $this->parseFormatVariables($m[3], $this->getConstants());
         switch ($m[1]) {
             case 'set':
                 if (array_key_exists($m[2], $params)) {
                     continue;
                 }
                 $val = $this->parseFormatVariablesAndFilters($m[3], $params);
                 $params[$m[2]] = $val;
                 break;
             case 'setGlobal':
                 $templateSetGlobals[$m[2]] = $m[3];
                 break;
             case 'appendGlobal':
                 $val = $this->parseFormatVariablesAndFilters($m[3], $params);
                 if (!array_key_exists($m[2], $globals)) {
                     $templateSetGlobals[$m[2]] = $val;
                 } else {
                     $templateSetGlobals[$m[2]] = $globals[$m[2]];
                     $templateSetGlobals[$m[2]] .= $val;
                 }
                 break;
         }
     }
     //        $this->Logger->debug(__CLASS__,$params);
     $template->setLocals($params);
     $template->setTemplateSetGlobals($templateSetGlobals);
     if (!empty($params['CacheTime'])) {
         $template->setCacheTime($params['CacheTime']);
     }
     if ($template->getCacheTime() > 0 && (empty($params['NoCache']) || StringUtils::strToBool($params['NoCache']) == false)) {
         // if this module is not marked as NoCache
         $template->setCacheable(true);
     }
     if ($this->benchmarkRendering) {
         $this->Benchmark->end('load-template-extended-' . $template->getName());
     }
     return $template;
 }