Example #1
0
 public function processTemplate(Template $template, $contentType, &$globals, $isFinalPass = false, $isDependentPass = false)
 {
     if (!$isFinalPass && $template->isDeferred() || $isDependentPass && !$template->isDependent()) {
         $this->Logger->debug("Deferring template [{$template->getName()}]");
         // defer my execution
         return $template->getMatchString();
     }
     $this->Logger->debug("Processing template [{$template->getName()}]");
     if ($this->benchmarkRendering) {
         $this->Benchmark->start('process-template-' . $template->getName());
     }
     if ($this->noTemplatesCalled == true) {
         $this->topTemplate = $template;
         $this->noTemplatesCalled = false;
         $template->setTopTemplate(true);
     }
     $templateEngine = $this->getTemplateEngine($template->getName());
     try {
         $template = $templateEngine->loadTemplateExtended($template, $globals);
     } catch (Exception $e) {
         if ($this->throwRenderExceptions) {
             throw $e;
         } else {
             $this->Logger->error($e->getMessage() . "\nURL: " . URLUtils::fullUrl());
             return '';
         }
     }
     $cacheThisModule = false;
     $usedCache = false;
     if ($this->cacheEnabled && $template->isCacheable()) {
         $cacheThisModule = true;
     }
     if ($cacheThisModule) {
         $cacheKey = $this->TemplateCache->getTemplateCacheKey($template, $globals);
         if (!in_array($template->getName(), (array) $this->pushCache) && ($cachedTemplate = $this->TemplateCache->get($cacheKey))) {
             // check the timestamp
             //if ($cachedTemplate->getTimestamp() == $template->getTimestamp()) {
             $this->Logger->debug("Loaded from cache [" . $template->getName() . "]");
             $locals = $template->Locals;
             // load from cache
             $template = $templateEngine->loadFromCache($cachedTemplate);
             $template->Locals = $locals;
             $content = $template->getProcessedContent();
             $usedCache = true;
             //}
         }
     }
     if (!$usedCache) {
         $this->Logger->debug("Processing [{$template->getName()}]");
         // process the template and all dependent includes
         $content = $templateEngine->processTemplateContents($template, $globals, $this, $isFinalPass, $isDependentPass);
         $template->setProcessedContent($content);
         if ($cacheThisModule) {
             // snapshot of the cache contents at this moment
             $toBeCachedTemplate = clone $templateEngine->prepareForCache($template);
         }
     }
     // parse all independent includes, outside of caching (recurse!)
     $content = $templateEngine->parseTemplateIncludes($content, $contentType, $template, $globals, $this, $isFinalPass, false);
     if (!$usedCache) {
         $this->Logger->debug("Processing template set globals [{$template->getName()}]...");
         // parse template set globals
         $templateSetGlobals = $templateEngine->processTemplateSetGlobals($template, $globals, $isFinalPass);
     } else {
         $templateSetGlobals = $cachedTemplate->getTemplateSetGlobals();
     }
     $globals = array_merge($globals, $templateSetGlobals);
     // store in cache
     if (!$usedCache && $cacheThisModule) {
         $toBeCachedTemplate->setIndependentTemplates($template->isIndependentTemplates());
         $toBeCachedTemplate->setParsedIndependent(true);
         if ($template->isTopTemplate()) {
             $toBeCachedTemplate->setTemplateSetGlobals($globals);
         } else {
             $toBeCachedTemplate->setTemplateSetGlobals($templateSetGlobals);
         }
         $this->Logger->debug("Storing in cache [" . $toBeCachedTemplate->getName() . "]");
         unset($toBeCachedTemplate->Locals);
         unset($toBeCachedTemplate->Data);
         unset($toBeCachedTemplate->Contents);
         unset($toBeCachedTemplate->SetMatches);
         unset($toBeCachedTemplate->File);
         unset($toBeCachedTemplate->TemplateBlocks);
         $this->TemplateCache->putTemplate($cacheKey, $toBeCachedTemplate);
     }
     if ($this->benchmarkRendering) {
         $this->Benchmark->end('process-template-' . $template->getName());
     }
     return $content;
 }