* @author Kornel LesiÅ„ski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: PHPTAL.php 775 2009-10-26 01:31:51Z kornel $ * @link http://phptal.org/ */ define('PHPTAL_VERSION', '1_2_1'); /* 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/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; /** * @see getPreFilters() */ protected $_prefilters = array(); /** * Prefilters have been redesigned. Old property is no longer used. * * @deprecated */ private $_prefilter = 'REMOVED: DO NOT USE'; protected $_postfilter = null; /** * list of template source repositories given to file source resolver */ protected $_repositories = array(); /** * template path (path that has been set, not necessarily loaded) */ protected $_path = null; /** * template source resolvers (classes that search for templates by name) */ 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 = MAIN_CHARSET; /** * type of syntax used in generated templates */ protected $_outputMode = PHPTAL::XHTML; /** * should all comments be stripped */ // 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 $externalMacroTemplatesCache = 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) { // äîáàâëåíî if ( MAIN_TEST==1 ){ $path = MAIN_SOURCE_PATH.'/tmpl/xml/test.xml'; $this->_outputMode = 'PHPTAL::XML'; } $this->_path = $path; $this->_globalContext = new StdClass(); $this->_context = new PHPTAL_Context(); $this->_context->setGlobal($this->_globalContext); $this->setPhpCodeDestination( MAIN_PATH.'/tmp/tmpl'); /*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) { // äîáàâëåíî if ( MAIN_TEST==1 ){ $path = MAIN_SOURCE_PATH.'/tmpl/xml/test.xml'; $this->_outputMode = 'PHPTAL::XML'; } $this->_prepared = false; $this->_functionName = null; $this->_codeFile = null; $this->_path = $path; $this->_source = null; $this->_context->_docType = null; $this->_context->_xmlDeclaration = 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 setSnippet($module,$func,$edit=true){ global $MAIN_DB; $this -> snippet = true; $this -> module = $module; $this -> func = $func; include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.snippets.php'); $s = new snippetsQuery('q'); // $s -> set_debug(1); $s -> where_func($func); $s -> where_module($module); $s -> where_lang(MAIN_USER_LANG); $s -> get('snippet,name'); list($snippet,$name) = $s -> row(); /*$sql = "SELECT snippet,name FROM ".MAIN_SNIPPETS_TBL." WHERE f_func='".$func."' AND m_path='".$module."'" ; list($snippet,$name) = $MAIN_DB -> fetch_array( $MAIN_DB -> query($sql) ); */ if ( $edit===true && MAIN_DEVELOPE_MODE==1 ){ //return $this -> setSource($snippet); return $this -> setSource('
'.$snippet.''); }else{ return $this -> setSource($snippet); } } public function setSource($src, $path=false) { if (!class_exists('PHPTAL_StringSource')) self::autoload('PHPTAL_StringSource'); if (!$path) { // this prefix tells string source that path has been faked $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; $this->_context->_docType = null; $this->_context->_xmlDeclaration = null; 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