silverstripe-framework/View/SSViewer_FromString.php
Damian Mooyman 8dd644d25d
API Namespace all classes
Namespace all templates
Move difflib and BBCodeParser2 to thirdparty
Remove deprecated API marked for removal in 4.0
2016-09-08 10:23:17 +12:00

85 lines
1.7 KiB
PHP

<?php
namespace SilverStripe\View;
use SilverStripe\Core\Config\Config;
/**
* Special SSViewer that will process a template passed as a string, rather than a filename.
*/
class SSViewer_FromString extends SSViewer
{
/**
* The global template caching behaviour if no instance override is specified
* @config
* @var bool
*/
private static $cache_template = true;
/**
* The template to use
* @var string
*/
protected $content;
/**
* Indicates whether templates should be cached
*
* @var bool
*/
protected $cacheTemplate;
public function __construct($content, TemplateParser $parser = null)
{
if ($parser) {
$this->setParser($parser);
}
$this->content = $content;
}
public function process($item, $arguments = null, $scope = null)
{
$hash = sha1($this->content);
$cacheFile = TEMP_FOLDER . "/.cache.$hash";
if (!file_exists($cacheFile) || isset($_GET['flush'])) {
$content = $this->parseTemplateContent($this->content, "string sha1=$hash");
$fh = fopen($cacheFile, 'w');
fwrite($fh, $content);
fclose($fh);
}
$val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, null, $scope);
if ($this->cacheTemplate !== null) {
$cacheTemplate = $this->cacheTemplate;
} else {
$cacheTemplate = Config::inst()->get('SilverStripe\\View\\SSViewer_FromString', 'cache_template');
}
if (!$cacheTemplate) {
unlink($cacheFile);
}
return $val;
}
/**
* @param boolean $cacheTemplate
*/
public function setCacheTemplate($cacheTemplate)
{
$this->cacheTemplate = (bool)$cacheTemplate;
}
/**
* @return boolean
*/
public function getCacheTemplate()
{
return $this->cacheTemplate;
}
}