Exemple #1
0
 /**
  * The process() method handles the "meat" of the template processing.
  * It takes care of caching the output (via {@link SS_Cache}),
  * as well as replacing the special "$Content" and "$Layout"
  * placeholders with their respective subtemplates.
  * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}.
  * 
  * Note: You can call this method indirectly by {@link ViewableData->renderWith()}.
  * 
  * @param ViewableData $item
  * @param SS_Cache $cache Optional cache backend
  * @return String Parsed template output.
  */
 public function process($item, $arguments = null)
 {
     SSViewer::$topLevel[] = $item;
     if ($arguments && $arguments instanceof Zend_Cache_Core) {
         Deprecation::notice('3.0', 'Use setPartialCacheStore to override the partial cache storage backend, ' . 'the second argument to process is now an array of variables.');
         $this->setPartialCacheStore($arguments);
         $arguments = null;
     }
     if (isset($this->chosenTemplates['main'])) {
         $template = $this->chosenTemplates['main'];
     } else {
         $keys = array_keys($this->chosenTemplates);
         $key = reset($keys);
         $template = $this->chosenTemplates[$key];
     }
     $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\', '/', ':'), '.', Director::makeRelative(realpath($template)));
     $lastEdited = filemtime($template);
     if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited || isset($_GET['flush'])) {
         $content = file_get_contents($template);
         $content = SSViewer::parseTemplateContent($content, $template);
         $fh = fopen($cacheFile, 'w');
         fwrite($fh, $content);
         fclose($fh);
     }
     $underlay = array('I18NNamespace' => basename($template));
     // Makes the rendered sub-templates available on the parent item,
     // through $Content and $Layout placeholders.
     foreach (array('Content', 'Layout') as $subtemplate) {
         if (isset($this->chosenTemplates[$subtemplate])) {
             $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]);
             $subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore());
             $underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments);
         }
     }
     $val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay);
     $output = Requirements::includeInHTML($template, $val);
     array_pop(SSViewer::$topLevel);
     // If we have our crazy base tag, then fix # links referencing the current page.
     if ($this->rewriteHashlinks && self::$options['rewriteHashlinks']) {
         if (strpos($output, '<base') !== false) {
             if (SSViewer::$options['rewriteHashlinks'] === 'php') {
                 $thisURLRelativeToBase = "<?php echo strip_tags(\$_SERVER['REQUEST_URI']); ?>";
             } else {
                 $thisURLRelativeToBase = strip_tags($_SERVER['REQUEST_URI']);
             }
             $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output);
         }
     }
     return $output;
 }
Exemple #2
0
 /**
  * The process() method handles the "meat" of the template processing.
  *
  * It takes care of caching the output (via {@link SS_Cache}), as well as 
  * replacing the special "$Content" and "$Layout" placeholders with their 
  * respective subtemplates.
  *
  * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}.
  * 
  * Note: You can call this method indirectly by {@link ViewableData->renderWith()}.
  * 
  * @param ViewableData $item
  * @param array|null $arguments - arguments to an included template
  * @param Object $inheritedScope - the current scope of a parent template including a sub-template
  *
  * @return HTMLText Parsed template output.
  */
 public function process($item, $arguments = null, $inheritedScope = null)
 {
     SSViewer::$topLevel[] = $item;
     if (isset($this->chosenTemplates['main'])) {
         $template = $this->chosenTemplates['main'];
     } else {
         $keys = array_keys($this->chosenTemplates);
         $key = reset($keys);
         $template = $this->chosenTemplates[$key];
     }
     $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\', '/', ':'), '.', Director::makeRelative(realpath($template)));
     $lastEdited = filemtime($template);
     if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited) {
         $content = file_get_contents($template);
         $content = $this->parseTemplateContent($content, $template);
         $fh = fopen($cacheFile, 'w');
         fwrite($fh, $content);
         fclose($fh);
     }
     $underlay = array('I18NNamespace' => basename($template));
     // Makes the rendered sub-templates available on the parent item,
     // through $Content and $Layout placeholders.
     foreach (array('Content', 'Layout') as $subtemplate) {
         if (isset($this->chosenTemplates[$subtemplate])) {
             $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate], $this->parser);
             $subtemplateViewer->includeRequirements(false);
             $subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore());
             $underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments);
         }
     }
     $output = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay, $inheritedScope);
     if ($this->includeRequirements) {
         $output = Requirements::includeInHTML($template, $output);
     }
     array_pop(SSViewer::$topLevel);
     // If we have our crazy base tag, then fix # links referencing the current page.
     $rewrite = Config::inst()->get('SSViewer', 'rewrite_hash_links');
     if ($this->rewriteHashlinks && $rewrite) {
         if (strpos($output, '<base') !== false) {
             if ($rewrite === 'php') {
                 $thisURLRelativeToBase = "<?php echo Convert::raw2att(preg_replace(\"/^(\\\\/)+/\", \"/\", \$_SERVER['REQUEST_URI'])); ?>";
             } else {
                 $thisURLRelativeToBase = Convert::raw2att(preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI']));
             }
             $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output);
         }
     }
     return DBField::create_field('HTMLText', $output, null, array('shortcodes' => false));
 }