* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: PHPTAL.php 665 2009-07-10 23:11:17Z kornel $ * @link http://phptal.org/ */ define('PHPTAL_VERSION', '1_2_0'); /* If you want to use autoload, comment out all lines starting with require_once 'PHPTAL and uncomment the line below: */ // spl_autoload_register(array('PHPTAL','autoload')); PHPTAL::setIncludePath(); require_once 'PHPTAL/Source.php'; require_once 'PHPTAL/SourceResolver.php'; require_once 'PHPTAL/FileSource.php'; require_once 'PHPTAL/RepeatController.php'; require_once 'PHPTAL/Context.php'; require_once 'PHPTAL/Exception.php'; require_once 'PHPTAL/Filter.php'; PHPTAL::restoreIncludePath(); /** * PHPTAL template entry point. * * * title = 'Welcome here'; * $tpl->result = range(1, 100); * ... * echo $tpl->execute(); * } * catch (Exception $e) { * echo $e; * } * ?> * * */ class PHPTAL { //{{{ /** * constants for output mode * @see setOutputMode() */ const XHTML = 11; const XML = 22; const HTML5 = 55; protected $_prefilter = null; protected $_postfilter = null; /** * list of template source repositories */ protected $_repositories = array(); /** * template path */ protected $_path = null; /** * template source resolvers */ protected $_resolvers = array(); /** * template source (only set when not working with file) */ protected $_source = null; /** * destination of PHP intermediate file */ protected $_codeFile = null; /** * php function generated for the template */ protected $_functionName = null; /** * set to true when template is ready for execution */ protected $_prepared = false; /** * associative array of phptal:id => PHPTAL_Trigger */ protected $_triggers = array(); /** * i18n translator */ protected $_translator = null; /** * global execution context */ protected $_globalContext = null; /** * current execution context */ protected $_context = null; /** * current template file (changes within macros) */ public $_file = false; /** * list of on-error caught exceptions */ protected $_errors = array(); /** * encoding used throughout */ protected $_encoding = 'UTF-8'; /** * type of syntax used in generated templates */ protected $_outputMode = PHPTAL::XHTML; /** * should all comments be stripped */ protected $_stripComments = false; // configuration properties /** * don't use code cache */ protected $_forceReparse = null; /** * directory where code cache is */ protected $_phpCodeDestination; protected $_phpCodeExtension = 'php'; /** * number of days */ protected $_cacheLifetime = 30; /** * 1/x */ protected $_cachePurgeFrequency = 30; /** * speeds up calls to external templates */ private $externalMacroTempaltesCache = array(); /** * restore_include_path() resets path to default in ini, * breaking application's custom paths, so a custom backup is necessary. */ private static $include_path_backup; /** * keeps track of multiple calls to setIncludePath() */ private static $include_path_set_nesting = 0; //}}} /** * PHPTAL Constructor. * * @param string $path Template file path. */ public function __construct($path=false) { $this->_path = $path; $this->_globalContext = new StdClass(); $this->_context = new PHPTAL_Context(); $this->_context->setGlobal($this->_globalContext); if (function_exists('sys_get_temp_dir')) { $this->setPhpCodeDestination(sys_get_temp_dir()); } elseif (substr(PHP_OS, 0, 3) == 'WIN') { if (file_exists('c:\\WINNT\\Temp\\')) { $this->setPhpCodeDestination('c:\\WINNT\\Temp'); } else { $this->setPhpCodeDestination('c:\\WINDOWS\\Temp\\'); } } else { $this->setPhpCodeDestination('/tmp/'); } } /** * create * returns a new PHPTAL object * * @param string $path Template file path. * * @return PHPTAL */ public static function create($path=false) { return new PHPTAL($path); } /** * Clone template state and context. * * @return void */ public function __clone() { $context = $this->_context; $this->_context = clone $this->_context; $this->_context->setParent($context); $this->_context->setGlobal($this->_globalContext); } /** * Set template from file path. * * @param string $path filesystem path, or any path that will be accepted by source resolver * @return $this */ public function setTemplate($path) { $this->_prepared = false; $this->_functionName = null; $this->_codeFile = null; $this->_path = $path; $this->_source = null; return $this; } /** * Set template from source. * * Should be used only with temporary template sources. * Use setTemplate() or addSourceResolver() whenever possible. * * @param string $src The phptal template source. * @param string $path Fake and 'unique' template path. * @return $this */ public function setSource($src, $path=false) { PHPTAL::setIncludePath(); require_once 'PHPTAL/StringSource.php'; PHPTAL::restoreIncludePath(); if (!$path) { $path = PHPTAL_StringSource::NO_PATH_PREFIX.md5($src).'>'; } $this->_prepared = false; $this->_functionName = null; $this->_codeFile = null; $this->_source = new PHPTAL_StringSource($src, $path); $this->_path = $path; return $this; } /** * Specify where to look for templates. * * @param mixed $rep string or Array of repositories * @return $this */ public function setTemplateRepository($rep) { if (is_array($rep)) { $this->_repositories = $rep; } else { $this->_repositories[] = $rep; } return $this; } /** * Get template repositories. */ public function getTemplateRepositories() { return $this->_repositories; } /** * Clears the template repositories. */ public function clearTemplateRepositories() { $this->_repositories = array(); return $this; } /** * Specify how to look for templates. * * @param $resolver PHPTAL_SourceResolver * @return $this */ public function addSourceResolver(PHPTAL_SourceResolver $resolver) { $this->_resolvers[] = $resolver; return $this; } /** * Ignore XML/XHTML comments on parsing. * Comments starting with