<?php
-class IPF_Template
+abstract class IPF_Template
{
- private $tpl;
- private $environment;
+ protected $environment;
- function __construct($template, $environment=null)
+ public function __construct($environment)
{
- $this->tpl = $template;
-
- if ($environment)
- $this->environment = $environment;
- else
- $this->environment = IPF_Template_Environment::getDefault();
+ $this->environment = $environment;
}
- public function __toString()
- {
- return $this->tpl;
- }
+ abstract public function __toString();
+
+ abstract protected function content();
public function compile()
{
- $compiler = new IPF_Template_Compiler($this->tpl, $this->environment);
+ $compiler = new IPF_Template_Compiler($this->content(), $this->environment);
return $compiler->getCompiledTemplate();
}
protected $_transStack = array();
protected $_transPlural = false;
- protected $_sourceFile;
-
protected $_currentTag;
private $environment;
public $_extendBlocks = array();
- public $_extendedTemplate = '';
-
- function __construct($template_file, $environment, $load=true)
+ function __construct($templateContent, $environment)
{
$allowedtags = IPF::get('template_tags', array());
$this->_allowedTags = array_merge($allowedtags, $this->_allowedTags);
foreach ($this->_allowedTags as $name=>$model) {
$this->_extraTags[$name] = new $model();
}
- $this->_sourceFile = $template_file;
$this->_allowedInVar = array_merge($this->_vartype, $this->_op);
$this->_allowedInExpr = array_merge($this->_vartype, $this->_op);
$this->_allowedAssign = array_merge($this->_vartype, $this->_assignOp,
$this->_op);
+
+ $this->templateContent = $templateContent;
$this->environment = $environment;
- if ($load) {
- $this->templateContent = $this->environment->loadTemplateFile($template_file);
- }
}
function compile()
function compileBlocks()
{
$tplcontent = $this->templateContent;
- $this->_extendedTemplate = '';
+ $extendedTemplate = '';
// Match extends on the first line of the template
if (preg_match("!{extends\s['\"](.*?)['\"]}!", $tplcontent, $_match)) {
- $this->_extendedTemplate = $_match[1];
+ $extendedTemplate = $_match[1];
}
// Get the blocks in the current template
$blocks = self::toplevelBlocks($this->templateContent);
}
}
}
- if (strlen($this->_extendedTemplate) > 0) {
+ if (strlen($extendedTemplate) > 0) {
// The template of interest is now the extended template
// as we are not in a base template
- $this->templateContent = $this->environment->loadTemplateFile($this->_extendedTemplate);
- $this->_sourceFile = $this->_extendedTemplate;
+ $this->templateContent = $this->environment->loadTemplateFile($extendedTemplate);
$this->compileBlocks(); //It will recurse to the base template.
} else {
// Replace the current blocks by a place holder
$res = '$_b_t_s=ob_get_contents(); ob_end_clean(); ob_start(); ';
break;
case 'include':
- // XXX fixme: Will need some security check, when online editing.
$argfct = preg_replace('!^[\'"](.*)[\'"]$!', '$1', $args);
- $_comp = new IPF_Template_Compiler($argfct, $this->environment);
+ $includedTemplateContent = $this->environment->loadTemplateFile($argfct);
+ $_comp = new IPF_Template_Compiler($includedTemplateContent, $this->environment);
$res = $_comp->compile();
$this->updateModifierStack($_comp);
break;
<?php
-class IPF_Template_Context
+class IPF_Template_Context
{
public $_vars;
$this->_vars[$var] = $value;
}
}
+
<?php
-class IPF_Template_Environment
+abstract class IPF_Template_Environment
{
- public $folders = array();
- public $cache = '';
+ abstract public function loadTemplateFile($filename);
- public function __construct($folders, $cache)
- {
- $this->folders = $folders;
- $this->cache = $cache;
- }
-
- public function loadTemplateFile($filename)
- {
- // FIXME: Very small security check, could be better.
- if (strpos($filename, '..') !== false) {
- throw new IPF_Exception(sprintf(__('Template file contains invalid characters: %s'), $filename));
- }
- foreach ($this->folders as $folder) {
- if (file_exists($folder.'/'.$filename)) {
- return file_get_contents($folder.'/'.$filename);
- }
- }
- throw new IPF_Exception(sprintf(__('Template file not found: %s'), $filename));
- }
-
- public function getCompiledTemplateName($template)
- {
- $_tmp = var_export($this->folders, true);
- $filename = $this->cache.'/IPF_Template-'.md5($_tmp.(string)$template).'.phps';
- if (IPF::get('debug') or !file_exists($filename)) {
- $this->write($filename, $template->compile());
- }
- return $filename;
- }
-
- private function write($filename, $content)
- {
- $fp = @fopen($filename, 'a');
- if ($fp !== false) {
- flock($fp, LOCK_EX);
- ftruncate($fp, 0);
- rewind($fp);
- fwrite($fp, $content, strlen($content));
- flock($fp, LOCK_UN);
- fclose($fp);
- @chmod($filename, 0777);
- return true;
- } else {
- throw new IPF_Exception_Template(sprintf(__('Cannot write the compiled template: %s'), $filename));
- }
- return false;
- }
+ abstract public function getCompiledTemplateName($template);
private static $defaultEnvironment = null;
public static function getDefault()
{
if (!self::$defaultEnvironment)
- self::$defaultEnvironment = new IPF_Template_Environment(IPF::get('template_dirs'), IPF::get('tmp'));
+ self::$defaultEnvironment = new IPF_Template_Environment_FileSystem(IPF::get('template_dirs'), IPF::get('tmp'));
return self::$defaultEnvironment;
}
}
--- /dev/null
+<?php
+
+class IPF_Template_Environment_FileSystem extends IPF_Template_Environment
+{
+ public $folders = array();
+ public $cache = '';
+
+ public function __construct($folders, $cache)
+ {
+ $this->folders = $folders;
+ $this->cache = $cache;
+ }
+
+ public function loadTemplateFile($filename)
+ {
+ // FIXME: Very small security check, could be better.
+ if (strpos($filename, '..') !== false) {
+ throw new IPF_Exception(sprintf(__('Template file contains invalid characters: %s'), $filename));
+ }
+ foreach ($this->folders as $folder) {
+ if (file_exists($folder.'/'.$filename)) {
+ return file_get_contents($folder.'/'.$filename);
+ }
+ }
+ throw new IPF_Exception(sprintf(__('Template file not found: %s'), $filename));
+ }
+
+ public function getCompiledTemplateName($template)
+ {
+ $_tmp = var_export($this->folders, true);
+ $filename = $this->cache.'/IPF_Template-'.md5($_tmp.(string)$template).'.phps';
+ if (IPF::get('debug') or !file_exists($filename)) {
+ $this->write($filename, $template->compile());
+ }
+ return $filename;
+ }
+
+ private function write($filename, $content)
+ {
+ $fp = @fopen($filename, 'a');
+ if ($fp !== false) {
+ flock($fp, LOCK_EX);
+ ftruncate($fp, 0);
+ rewind($fp);
+ fwrite($fp, $content, strlen($content));
+ flock($fp, LOCK_UN);
+ fclose($fp);
+ @chmod($filename, 0777);
+ return true;
+ } else {
+ throw new IPF_Exception_Template(sprintf(__('Cannot write the compiled template: %s'), $filename));
+ }
+ return false;
+ }
+}
+
--- /dev/null
+<?php
+
+class IPF_Template_File extends IPF_Template
+{
+ private $tpl;
+
+ function __construct($template, $environment=null)
+ {
+ parent::__construct($environment ? $environment : IPF_Template_Environment::getDefault());
+ $this->tpl = $template;
+ }
+
+ public function __toString()
+ {
+ return $this->tpl;
+ }
+
+ protected function content()
+ {
+ return $this->environment->loadTemplateFile($this->tpl);
+ }
+}
+
--- /dev/null
+<?php
+
+class IPF_Template_String extends IPF_Template
+{
+ private $tpl;
+
+ function __construct($template, $environment=null)
+ {
+ parent::__construct($environment ? $environment : IPF_Template_Environment::getDefault());
+ $this->tpl = $template;
+ }
+
+ public function __toString()
+ {
+ return $this->tpl;
+ }
+
+ protected function content()
+ {
+ return $this->tpl;
+ }
+}
+