📄 pattemplate.php
字号:
* @param array parameters for the input filter * @return boolean true on success, patError otherwise */ function applyInputFilter( $filter, $params = array() ) { if( !is_object( $filter ) ) { $filter = &$this->loadModule( 'InputFilter', $filter, $params ); } if( patErrorManager::isError( $filter ) ) return $filter; $this->_inputFilters[] = &$filter; return true; } /** * open a file and parse for patTemplate tags * * @access public * @param name of the file * @return true, if the template could be parsed * @deprecated Use patTemplate::readTemplatesFromInput() instead, as the method name is misleading * @see readTemplatesFromInput() */ function readTemplatesFromFile( $filename ) { return $this->readTemplatesFromInput( $filename, 'File' ); } /** * open any input and parse for patTemplate tags * * @access public * @param string name of the input (filename, shm segment, etc.) * @param string driver that is used as reader, you may also pass a Reader object * @param array additional options that will only be used for this template * @param string name of the template that should be used as a container, should not be used by public * calls. * @return boolean true, if the template could be parsed, false otherwise */ function readTemplatesFromInput( $input, $reader = 'File', $options = null, $parseInto = null ) { if ((string)$input === '') { return patErrorManager::raiseError(PATTEMPLATE_ERROR_NO_INPUT, 'No input to read has been passed.'); } if (is_array($options)) { $options = array_merge( $this->_options, $options ); } else { $options = $this->_options; } if (!is_null($parseInto)) { $parseInto = strtolower( $parseInto ); } $templates = false; if ($this->_tmplCache !== null) { /** * get the unique cache key */ $key = $this->_tmplCache->getKey($input, $options); $templates = $this->_loadTemplatesFromCache( $input, $reader, $options, $key ); /** * check for error returned from cache */ if (patErrorManager::isError($templates)) { return $templates; } } /** * templates have not been loaded from cache */ if ($templates === false) { if (!is_object( $reader)) { $reader = &$this->loadModule('Reader', $reader); if (patErrorManager::isError($reader)) { return $reader; } } if ($reader->isInUse()) { $reader = &$this->loadModule( 'Reader', $reader->getName(), array(), true); if( patErrorManager::isError( $reader ) ) { return $reader; } } $reader->setOptions($options); /** * set the root attributes */ if( !is_null( $parseInto ) ) { $attributes = $this->getAttributes( $parseInto ); if( !patErrorManager::isError( $attributes ) ) { $reader->setRootAttributes( $attributes ); } } $templates = $reader->readTemplates( $input ); /** * check for error returned from reader */ if( patErrorManager::isError( $templates ) ) return $templates; /** * store the */ if( $this->_tmplCache !== null ) { $this->_tmplCache->write( $key, $templates ); } } /** * traverse all templates */ foreach( $templates as $name => $spec ) { /** * root template */ if( $name == '__ptroot' ) { if( $parseInto === false ) { continue; } if( !in_array( $parseInto, $this->_templateList ) ) continue; $spec['loaded'] = true; $spec['attributes'] = $this->_templates[$parseInto]['attributes']; $name = $parseInto; } else { /** * store the name */ array_push( $this->_templateList, $name ); } /** * if this is the first template that has been loaded * set it as the root template */ if( $this->_root === null && is_null( $parseInto ) && isset( $spec['isRoot'] ) && $spec['isRoot'] == true ) { $this->_root = $name; } /** * set some default values */ $spec['iteration'] = 0; $spec['lastMode'] = 'w'; $spec['result'] = ''; $spec['modifyVars'] = array(); $spec['copyVars'] = array(); $spec['defaultVars'] = array(); /** * store the template */ $this->_templates[$name] = $spec; $this->prepareTemplate( $name ); /** * store the default values of the variables */ foreach( $spec['varspecs'] as $varname => $varspec ) { if (isset($varspec['modifier'])) { $this->_templates[$name]['modifyVars'][$varname] = $varspec['modifier']; } if( isset( $varspec['copyfrom'] ) ) { $this->_templates[$name]['copyVars'][$varname] = $varspec['copyfrom']; } if( !isset( $varspec['default'] ) ) continue; $this->_templates[$name]['defaultVars'][$varname] = $varspec['default']; if( !is_null( $this->getVar( $name, $varname ) ) ) continue; $this->addVar( $name, $varname, $varspec['default'] ); } unset($this->_templates[$name]['varspecs']); /** * autoload the template * * Some error management is needed here... */ if( isset( $this->_templates[$name]['attributes']['src'] ) && $this->_templates[$name]['attributes']['autoload'] == 'on' ) { if( $this->_templates[$name]['loaded'] !== true ) { if( $this->_templates[$name]['attributes']['parse'] == 'on' ) { $this->readTemplatesFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], $options, $name ); } else { $this->loadTemplateFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], null, $name ); } $this->_templates[$name]['loaded'] = true; } } } return true; } /** * load from template cache * * @access private * @param string name of the input (filename, shm segment, etc.) * @param string driver that is used as reader, you may also pass a Reader object * @param array options for the reader * @param string cache key * @return array|boolean either an array containing the templates, or false */ function _loadTemplatesFromCache( $input, &$reader, $options, $key ) { if( is_object( $reader ) ) $statName = $reader->getName(); else $statName = $reader; $stat = &$this->loadModule( 'Stat', $statName ); $stat->setOptions( $options ); /** * get modification time */ $modTime = $stat->getModificationTime( $input ); $templates = $this->_tmplCache->load( $key, $modTime ); return $templates; } /** * open any input and load content into template * * @access public * @param string name of the input (filename, shm segment, etc.) * @param string driver that is used as reader * @param string name of the template that should be used as a container, * @return boolean true, if the template could be parsed, false otherwise */ function loadTemplateFromInput( $input, $reader = 'File', $options = null, $parseInto = false ) { if( is_array( $options ) ) $options = array_merge( $this->_options, $options ); else $options = $this->_options; if( !is_null( $parseInto ) ) $parseInto = strtolower( $parseInto ); $reader = &$this->loadModule( 'Reader', $reader ); if( patErrorManager::isError( $reader ) ) { return $reader; } $reader->setOptions($options); $result = $reader->loadTemplate( $input ); if( patErrorManager::isError( $result ) ) { return $result; } $this->_templates[$parseInto]['content'] .= $result; $this->_templates[$parseInto]['loaded'] = true; return true; } /** * load a template that had autoload="off" * * This is needed, if you change the source of a template and want to * load it, after changing the attribute. * * @access public * @param string template name * @return boolean true, if template could be loaded */ function loadTemplate( $template ) { $template = strtolower( $template ); if( !isset( $this->_templates[$template] ) ) { return patErrorManager::raiseWarning( PATTEMPLATE_WARNING_NO_TEMPLATE, "Template '$template' does not exist." ); } if( $this->_templates[$template]['loaded'] === true ) return true; if( $this->_templates[$template]['attributes']['parse'] == 'on' ) { return $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template ); } else { return $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template ); } } /** * loads a patTemplate module * * Modules are located in the patTemplate folder and include: * - Readers * - Caches * - Variable Modifiers * - Filters * - Functions * - Stats * * @access public * @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter) * @param string moduleName * @param array parameters for the module * @return object */ function &loadModule( $moduleType, $moduleName, $params = array(), $new = false ) { if( !isset( $this->_modules[$moduleType] ) ) $this->_modules[$moduleType] = array(); $sig = md5( $moduleName . serialize( $params ) ); if( isset( $this->_modules[$moduleType][$sig] ) && $new === false ) { return $this->_modules[$moduleType][$sig]; } if( !class_exists( 'patTemplate_Module' ) ) { $file = sprintf( "%s/Module.php", $this->getIncludePath() ); if( !file_exists( $file ) or !include_once $file ) return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, 'Could not load module base class.' ); } $baseClass = 'patTemplate_' . $moduleType; if( !class_exists( $baseClass ) ) { $baseFile = sprintf( "%s/%s.php", $this->getIncludePath(), $moduleType ); if( !file_exists( $baseFile ) or !include_once $baseFile ) return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, "Could not load base class for $moduleType ($baseFile)." ); } $moduleClass = 'patTemplate_' . $moduleType . '_' .$moduleName; if( !class_exists( $moduleClass ) ) { if( isset( $this->_moduleDirs[$moduleType] ) ) $dirs = $this->_moduleDirs[$moduleType]; else $dirs = array(); array_push( $dirs, $this->getIncludePath() .'/'. $moduleType ); $found = false; foreach( $dirs as $dir ) { $moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) ); if ( file_exists( $moduleFile ) and include_once $moduleFile) { $found = true; break; } } if( !$found ) { return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Could not load module $moduleClass ($moduleFile)." ); } } if( !class_exists( $moduleClass ) ) { return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Module file $moduleFile does not contain class $moduleClass." ); } $this->_modules[$moduleType][$sig] = &new $moduleClass; if( method_exists( $this->_modules[$moduleType][$sig], 'setTemplateReference' ) ) { $this->_modules[$moduleType][$sig]->setTemplateReference( $this ); } $this->_modules[$moduleType][$sig]->setParams( $params ); return $this->_modules[$moduleType][$sig]; } /** * checks whether a module exists. * * Modules are located in the patTemplate folder and include: * - Readers * - Caches * - Variable Modifiers * - Filters * - Functions * - Stats * * @access public * @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter) * @param string moduleName * @return boolean */ function moduleExists( $moduleType, $moduleName ) { // !!!JOOMLA VARIATION!!! // cache checks on files static $paths; if (!$paths) { $paths = array(); } if (isset($this->_moduleDirs[$moduleType])) { $dirs = $this->_moduleDirs[$moduleType]; } else { $dirs = array(); } array_push($dirs, $this->getIncludePath() .'/'. $moduleType); foreach ($dirs as $dir) { $moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) ); if (!isset( $paths[$moduleFile] )) { if (!file_exists($moduleFile)) { $paths[$moduleFile] = false; } else if (!is_readable($moduleFile)) { $paths[$moduleFile] = false; } else { $paths[$moduleFile] = true; } } if (!$paths[$moduleFile]) { continue; } return true; } return false; } /** * parses a template * * Parses a template and stores the parsed content. * mode can be "w" for write (delete already parsed content) or "a" for append (appends the * new parsed content to the already parsed content) * * @access public * @param string name of the template * @param string mode for the parsing */ function parseTemplate( $template, $mode = 'w' ) { $template = strtolower($template); if (!isset($this->_templates[$template])) { return patErrorManager::raiseWarning( PATTEMPLATE_WARNING_NO_TEMPLATE, "Template '$template' does not exist." ); } /** * template is not visible */ if ($this->_templates[$template]['attributes']['visibility'] == 'hidden') { $this->_templates[$template]['result'] = ''; $this->_templates[$template]['parsed'] = true; return true; } /** * check, if the template has been loaded * and load it if necessary. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -