📄 pattemplate.php
字号:
if ($this->_templates[$template]['loaded'] !== true) { if ($this->_templates[$template]['attributes']['parse'] == 'on') { $result = $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template ); } else { $result = $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template ); } if (patErrorManager::isError($result)) { return $result; } } /** * check for autoclear */ if( isset( $this->_templates[$template]['attributes']['autoclear'] ) && $this->_templates[$template]['attributes']['autoclear'] == 'yes' && $mode === 'w' && $this->_templates[$template]['lastMode'] != 'a' ) { $this->_templates[$template]['parsed'] = false; } /** * template has been parsed and mode is not 'append' */ if ($this->_templates[$template]['parsed'] === true && $mode === 'w') { return true; } $this->_templates[$template]['lastMode'] = $mode; $this->_initTemplate( $template ); if (!isset($this->_vars[$template]['rows'])) { $this->_vars[$template]['rows'] = array(); } $loop = count( $this->_vars[$template]['rows'] ); /** * loop at least one times */ if ($loop < 1) { $loop = 1; } if (isset($this->_templates[$template]['attributes']['maxloop'])) { $loop = ceil( $loop / $this->_templates[$template]['attributes']['maxloop'] ) * $this->_templates[$template]['attributes']['maxloop']; } $this->_templates[$template]['loop'] = max( $this->_templates[$template]['attributes']['loop'], $loop ); $start = 0; if (isset($this->_templates[$template]['attributes']['limit'])) { $p = strpos( $this->_templates[$template]['attributes']['limit'], ',' ); if ($p === false) { $this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $this->_templates[$template]['attributes']['limit'] ); $start = 0; } else { $start = substr( $this->_templates[$template]['attributes']['limit'], 0, $p ); $end = substr( $this->_templates[$template]['attributes']['limit'], $p+1 )+$start; $this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $end ); } } /** * template should be cleared before parsing */ if ($mode == 'w') { $this->_templates[$template]['result'] = ''; $this->_templates[$template]['iteration'] = $start; } $loopCount = 0; for ($i = $start; $i < $this->_templates[$template]['loop']; $i++) { $finished = false; unset( $this->_templates[$template]['vars'] ); /** * fetch the variables */ $this->_fetchVariables( $template ); /** * fetch the template */ $result = $this->_fetchTemplate($template); if ($result === false) { $this->_templates[$template]['iteration']++; continue; } /** * parse */ $this->_parseVariables( $template ); $result = $this->_parseDependencies( $template ); if (patErrorManager::isError($result)) { return $result; } /** * store result */ $this->_templates[$template]['result'] .= $this->_templates[$template]['work']; $this->_templates[$template]['iteration']++; ++$loopCount; /** * check for maximum loops */ if (isset($this->_templates[$template]['attributes']['maxloop'])) { if ($loopCount == $this->_templates[$template]['attributes']['maxloop'] && $i < ($loop-1)) { $loopCount = 0; $finished = true; $this->_templates[$template]['parsed'] = true; $this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a' ); $this->_templates[$template]['parsed'] = false; $this->_templates[$template]['result'] = ''; } } } if (!$finished && isset($this->_templates[$template]['attributes']['maxloop'])) { $this->_templates[$template]['parsed'] = true; $this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a', false ); $this->_templates[$template]['parsed'] = false; $this->_templates[$template]['result'] = ''; $this->_templates[$this->_templates[$template]['attributes']['parent']]['work'] = ''; } $this->_parseGlobals($template); $this->_handleUnusedVars($template); $this->_templates[$template]['parsed'] = true; if (isset($this->_templates[$template]['attributes']['autoclear']) && $this->_templates[$template]['attributes']['autoclear'] == 'yes') { $this->_vars[$template] = array( 'scalar' => array(), 'rows' => array() ); } if (isset($this->_templates[$template]['attributes']['outputfilter'])) { if (is_object($this->_templates[$template]['attributes']['outputfilter'])) { $filter = &$this->_templates[$template]['attributes']['outputfilter']; } else { $filter = &$this->loadModule('OutputFilter', $this->_templates[$template]['attributes']['outputfilter']); } if (patErrorManager::isError($filter)) { return $filter; } $this->_templates[$template]['result'] = $filter->apply($this->_templates[$template]['result']); } return true; } /** * Initialize a template * * This method checks the variable specifications and * copys variables from other templates. * * @access private * @param string name of the template * @return boolean true on success */ function _initTemplate( $template ) { foreach( $this->_templates[$template]['copyVars'] as $dest => $src ) { /** * copy from the same template */ if( !is_array( $src ) ) { $srcTemplate = $template; $srcVar = $src; } else { $srcTemplate = $src[0]; $srcVar = $src[1]; } $copied = false; /** * copy from another template */ if( isset( $this->_vars[$srcTemplate] ) ) { if( isset( $this->_vars[$srcTemplate]['scalar'][$srcVar] ) ) { $this->_vars[$template]['scalar'][$dest] = $this->_vars[$srcTemplate]['scalar'][$srcVar]; continue; } $rows = count( $this->_vars[$srcTemplate]['rows'] ); for( $i = 0; $i < $rows; $i++ ) { if( !isset( $this->_vars[$srcTemplate]['rows'][$i][$srcVar] ) ) continue; if( !isset( $this->_vars[$template]['rows'][$i] ) ) $this->_vars[$template]['rows'][$i] = array(); $this->_vars[$template]['rows'][$i][$dest] = $this->_vars[$srcTemplate]['rows'][$i][$srcVar]; $copied = true; } } if( !$copied && isset( $this->_globals[$srcVar] )) { $this->_vars[$template]['scalar'][$dest] = $this->_globals[$srcVar]; } } return true; } /** * parse all variables in a template * * @access private * @param string */ function _parseVariables( $template ) { /** * modify variables before parsing */ $this->_applyModifers($template, $this->_templates[$template]['vars']); foreach( $this->_templates[$template]['vars'] as $key => $value ) { if( is_array( $value ) ) { if( count( $this->_templates[$template]['currentDependencies'] ) == 1 ) { $child = $this->_templates[$template]['currentDependencies'][0]; } else { if( isset( $this->_templates[$template]['attributes']['child'] ) ) $child = $this->_templates[$template]['attributes']['child']; else continue; } $this->setAttribute( $child, 'autoclear', 'yes' ); $this->addVar( $child, $key, $value ); continue; } $var = $this->_startTag.$key.$this->_endTag; $this->_templates[$template]['work'] = str_replace( $var, $value, $this->_templates[$template]['work'] ); } return true; } /** * parse global variables in the template * * @access private * @param string name of the template * @return boolean */ function _parseGlobals($template) { $globalVars = $this->_globals; $this->_applyModifers($template, $globalVars); foreach( $globalVars as $key => $value ) { if( is_array( $value ) ) { continue; } $var = $this->_startTag.$key.$this->_endTag; $this->_templates[$template]['result'] = str_replace( $var, $value, $this->_templates[$template]['result'] ); } return true; } /** * apply variable modifiers * * The variables will be passed by reference. * * @access private * @param string name of the template (use modifiers from this template) * @param array variables to which the modifiers should be applied * @return boolean */ function _applyModifers($template, &$vars) { foreach ($this->_templates[$template]['modifyVars'] as $varname => $modifier) { if (!isset($vars[$varname])) { continue; } if (($modifier['type'] === 'php' || $modifier['type'] === 'auto' ) && is_callable($modifier['mod'])) { $vars[$varname] = call_user_func($modifier['mod'], $vars[$varname]); continue; } if ($modifier['type'] === 'php') { continue; } $mod = &$this->loadModule( 'Modifier', ucfirst( $modifier['mod'] ) ); $vars[$varname] = $mod->modify( $vars[$varname], $modifier['params'] ); } // apply the default modifier if (isset($this->_templates[$template]['attributes']['defaultmodifier'])) { $defaultModifier = $this->_templates[$template]['attributes']['defaultmodifier']; if (is_callable($defaultModifier)) { $type = 'php'; } else { $type = 'custom'; $defaultModifier = &$this->loadModule('Modifier', ucfirst($defaultModifier)); } foreach (array_keys($vars) as $varname) { if (isset($this->_templates[$template]['modifyVars'][$varname])) { continue; } if ($type === 'php') { $vars[$varname] = call_user_func($defaultModifier, $vars[$varname]); } else { $vars[$varname] = $defaultModifier->modify($vars[$varname], array()); } } } return true; } /** * parse all dependencies in a template * * @access private * @param string */ function _parseDependencies($template) { $countDep = count( $this->_templates[$template]['currentDependencies'] ); for ($i = 0; $i < $countDep; $i++) { $depTemplate = $this->_templates[$template]['currentDependencies'][$i]; if ($depTemplate == $template) { return patErrorManager::raiseError(PATTEMPLATE_ERROR_RECURSION, 'You have an error in your template "' . $template . '", which leads to recursion'); } $this->parseTemplate($depTemplate); $var = $this->_startTag.'TMPL:'.strtoupper( $depTemplate) .$this->_endTag; $this->_templates[$template]['work'] = str_replace( $var, $this->_templates[$depTemplate]['result'], $this->_templates[$template]['work'] ); } return true; } /** * fetch plain template * * The template content will be stored in the template * configuration so it can be used by other * methods. * * @access private * @param string template name * @return boolean */ function _fetchTemplate( $template ) { switch( $this->_templates[$template]['attributes']['type'] ) { /** * condition template */ case 'condition': $value = $this->_getConditionValue($template, $this->_templates[$template]['attributes']['conditionvar']); if ($value === false) { $this->_templates[$template]['work'] = ''; $this->_templates[$template]['currentDependencies'] = array(); } else { $this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data']; $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies']; } break; /** * condition template */ case 'simplecondition': foreach( $this->_templates[$template]['attributes']['requiredvars'] as $var ) { // different template scope if( $var[0] !== $template ) { $this->_fetchVariables($var[0]); } $value = null; // fetch the local variable if( isset( $this->_templates[$var[0]]['vars'][$var[1]] ) && strlen( $this->_templates[$var[0]]['vars'][$var[1]] ) > 0 ) { $value = $this->_templates[$var[0]]['vars'][$var[1]]; } if (isset($this->_templates[$template]['attributes']['useglobals'])) { if(isset($this->_globals[$var[1]]) && strlen($this->_globals[$var[1]]) > 1) { $value = $this->_globals[$var[1]]; } } if ($value !== null) { if ($var[2] === null) { continue; } else { // Joomla! addition 23-June-2005 // value wrapped in ## uses regex for comparison $condition = $var[2]; if (substr( $condition, 0, 1 ) == '#' && substr( $condition, -1, 1 ) == '#' ) { if (preg_match( $condition, $value )) { continue; } } else if ($condition == $value) { continue; } /* Pat Original if ($var[2] == $value) { continue; } */ } } $this->_templates[$template]['work'] = ''; $this->_templates[$template]['currentDependencies'] = array(); break 2; } $this->_templates[$template]['work'] = $this->_templates[$template]['content']; $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies']; break; /** * modulo template */ case 'modulo': // check for empty template if ($this->_hasVariables($template)) { $value = (string)($this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo']; } else { $value = '__empty'; } $value = $this->_getConditionValue($template, $value, false); if ($value === false) { $this->_templates[$template]['work'] = ''; $this->_templates[$template]['currentDependencies'] = array(); } else { $this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data']; $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies']; } break; /** * standard template */ default: $this->_templates[$template]['work'] = $this->_templates[$template]['content']; $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies']; break; } return true; } /** * check, whether a template contains variables * * @access private * @param string template name * @return boolean */ function _hasVariables($template) { if (!empty($this->_vars[$template]['scalar'])) { return true; } if (isset($this->_vars[$template]['rows'][$this->_templates[$template]['iteration']])) { return true; } return false; } /** * fetch the value of a condition variable * * _fetchVariables() has to be called before this * method is being called. * * @access private * @param string template name
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -