📄 pattemplate.php
字号:
* @param string condition value * @param boolean flag that indicates whether value is the name of the variable that should be resolved * * @todo split this method into smaller check methods that will be called according to * a priority list */ function _getConditionValue( $template, $value, $isVar = true ) { if ($isVar === true) { if (isset($this->_templates[$template]['attributes']['conditiontmpl'])) { $_template = $this->_templates[$template]['attributes']['conditiontmpl']; $this->_fetchVariables($_template); } else { $_template = $template; } /** * get the value from the template variables */ if (!isset($this->_templates[$_template]['vars'][$value]) || strlen($this->_templates[$_template]['vars'][$value]) === 0) { if ($this->_templates[$template]['attributes']['useglobals'] == 'yes' || $this->_templates[$template]['attributes']['useglobals'] == 'useglobals') { if (isset( $this->_globals[$value] ) && strlen( $this->_globals[$value] ) > 0) { $value = $this->_globals[$value]; } else { $value = '__empty'; } } else { $value = '__empty'; } } else { $value = $this->_templates[$_template]['vars'][$value]; } } else { $_template = $template; } // if value is empty and a template for empty has been defined, this // has priority if ($value === '__empty' && isset($this->_templates[$template]['subtemplates']['__empty'])) { return $value; } // only one iteration (but not empty), use the __single condition if ($value !== '__empty' && $this->_templates[$_template]['loop'] === 1) { if( isset($this->_templates[$template]['subtemplates']['__single'])) { return '__single'; } } else { // is __first? if( $this->_templates[$_template]['iteration'] == 0 ) { if( isset( $this->_templates[$template]['subtemplates']['__first'] ) ) { return '__first'; } } /** * is __last? */ if (isset($this->_templates[$_template]['loop'])) { $max = $this->_templates[$_template]['loop'] - 1; if( $this->_templates[$_template]['iteration'] == $max ) { if( isset( $this->_templates[$template]['subtemplates']['__last'] ) ) { return '__last'; } } } } // search for exact match foreach (array_keys($this->_templates[$template]['subtemplates']) as $key) { if (isset($this->_templates[$template]['subtemplates'][$key]['attributes']['var'])) { $var = $this->_templates[$template]['subtemplates'][$key]['attributes']['var']; if (isset($this->_templates[$template]['vars'][$var])) { $current = $this->_templates[$template]['vars'][$var]; } else { $current = null; } } else { $current = $key; } if ((string)$value === (string)$current) { return $key; } } /** * is __default? */ if( isset( $this->_templates[$template]['subtemplates']['__default'] ) ) { return '__default'; } return false; } /** * fetch variables for a template * * The variables will be stored in the template * configuration so they can be used by other * methods. * * @access private * @param string template name * @return boolean */ function _fetchVariables( $template ) { /** * variables already have been fetched */ if (isset($this->_templates[$template]['vars'])) { return true; } $iteration = $this->_templates[$template]['iteration']; $vars = array(); if( isset( $this->_templates[$template]['attributes']['varscope'] ) ) { if (!is_array($this->_templates[$template]['attributes']['varscope'])) { $this->_templates[$template]['attributes']['varscope'] = array($this->_templates[$template]['attributes']['varscope']); } foreach ($this->_templates[$template]['attributes']['varscope'] as $scopeTemplate) { if ($this->exists($scopeTemplate)) { $this->_fetchVariables( $scopeTemplate ); $vars = array_merge($this->_templates[$scopeTemplate]['vars'], $vars); } else { patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, 'Template \''.$scopeTemplate.'\' does not exist, referenced in varscope attribute of template \''.$template.'\''); } } } else { $vars = array(); } /** * get the scalar variables */ if( isset( $this->_vars[$template] ) && isset( $this->_vars[$template]['scalar'] ) ) { $vars = array_merge( $vars, $this->_vars[$template]['scalar'] ); } /** * get the row variables */ if( isset( $this->_vars[$template]['rows'][$iteration] ) ) { $vars = array_merge( $vars, $this->_vars[$template]['rows'][$iteration] ); } /** * add some system variables */ $currentRow = $iteration + $this->_templates[$template]['attributes']['rowoffset']; $vars['PAT_ROW_VAR'] = $currentRow; if( $this->_templates[$template]['attributes']['type'] == 'modulo' ) { $vars['PAT_MODULO_REP'] = ceil( $currentRow / $this->_templates[$template]['attributes']['modulo'] ); $vars['PAT_MODULO'] = ( $this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo']; } if( $this->_templates[$template]['attributes']['addsystemvars'] !== false ) { $vars['PATTEMPLATE_VERSION'] = $this->_systemVars['appVersion']; $vars['PAT_LOOPS'] = $this->_templates[$template]['loop']; switch ($this->_templates[$template]['attributes']['addsystemvars']) { case 'boolean': $trueValue = 'true'; $falseValue = 'false'; break; case 'integer': $trueValue = '1'; $falseValue = '0'; break; default: $trueValue = $this->_templates[$template]['attributes']['addsystemvars']; $falseValue = ''; break; } $vars['PAT_IS_ODD'] = ( $currentRow % 2 == 1 ) ? $trueValue : $falseValue; $vars['PAT_IS_EVEN'] = ( $currentRow % 2 == 0 ) ? $trueValue : $falseValue; $vars['PAT_IS_FIRST'] = ( $currentRow == 1 ) ? $trueValue : $falseValue; $vars['PAT_IS_LAST'] = ( $currentRow == $this->_templates[$template]['loop'] ) ? $trueValue : $falseValue; $vars['PAT_ROW_TYPE'] = ( $currentRow % 2 == 1 ) ? 'odd' : 'even'; } $this->_templates[$template]['vars'] = $vars; return true; } /** * handle all unused variables in a template * * This is influenced by the 'unusedvars' attribute of the * template * * @access private * @param string */ function _handleUnusedVars( $template ) { $regexp = '/([^\\\])('.$this->_startTag.'[^a-z]+[^\\\]'.$this->_endTag.')/U'; switch( $this->_templates[$template]['attributes']['unusedvars'] ) { case 'comment': $this->_templates[$template]['result'] = preg_replace( $regexp, '<!-- \\1\\2 -->', $this->_templates[$template]['result'] ); break; case 'strip': $this->_templates[$template]['result'] = preg_replace( $regexp, '\\1', $this->_templates[$template]['result'] ); break; case 'nbsp': $this->_templates[$template]['result'] = preg_replace( $regexp, '\\1 ', $this->_templates[$template]['result'] ); break; case 'ignore': break; default: $this->_templates[$template]['result'] = preg_replace( $regexp, '\\1'.$this->_templates[$template]['attributes']['unusedvars'], $this->_templates[$template]['result'] ); break; } // replace quoted variables $regexp = '/[\\\]'.$this->_startTag.'([^a-z]+)[\\\]'.$this->_endTag.'/U'; $this->_templates[$template]['result'] = preg_replace( $regexp, $this->_startTag.'\\1'.$this->_endTag, $this->_templates[$template]['result'] ); return true; } /** * returns a parsed Template * * If the template already has been parsed, it just returns the parsed template. * If the template has not been loaded, it will be loaded. * * @access public * @param string name of the template * @param boolean whether to apply output filters * @return string Content of the parsed template * @see displayParsedTemplate() */ function getParsedTemplate( $name = null, $applyFilters = false ) { if (is_null($name)) { $name = $this->_root; } $name = strtolower( $name ); $result = $this->parseTemplate( $name ); if (patErrorManager::isError( $result )) { return $result; } if ($applyFilters === false) { return $this->_templates[$name]['result']; } $result = $this->_templates[$name]['result']; $cnt = count ($this->_outputFilters); for ($i = 0; $i < $cnt; $i++) { $result = $this->_outputFilters[$i]->apply( $result ); } return $result; } /** * displays a parsed Template * * If the template has not been loaded, it will be loaded. * * @see getParsedTemplate() * @param string name of the template * @param boolean whether to apply output filters * @return boolean true on success * @access public */ function displayParsedTemplate($name = null, $applyFilters = true) { $result = $this->getParsedTemplate($name, $applyFilters); /** * error happened */ if (patErrorManager::isError($result)) { return $result; } echo $result; return true; } /** * parse a template and push the result into a variable of any other * template * * If the template already has been parsed, it will just be pushed into the variable. * If the template has not been loaded, it will be loaded. * * @access public * @param string name of the template * @return string Content of the parsed template * @param boolean if set to true, the value will be appended to the value already stored. * @see getParsedTemplate() * @see addVar() */ function parseIntoVar( $srcTmpl, $destTmpl, $var, $append = false ) { $srcTmpl = strtolower( $srcTmpl ); $destTmpl = strtolower( $destTmpl ); $var = strtoupper($var); $result = $this->parseTemplate( $srcTmpl ); if( patErrorManager::isError( $result ) ) return $result; if( $append !== true || !isset( $this->_vars[$destTmpl]['scalar'][$var] ) ) $this->_vars[$destTmpl]['scalar'][$var] = ''; $this->_vars[$destTmpl]['scalar'][$var] .= $this->_templates[$srcTmpl]['result']; return true; } /** * clears a parsed Template * * Parsed Content, variables and the loop attribute are cleared * * If you will not be using this template anymore, then you should * call freeTemplate() * * @access public * @param string name of the template * @param boolean set this to true to clear all child templates, too * @see clearAllTemplates() * @see freeTemplate() */ function clearTemplate( $name, $recursive = false ) { $name = strtolower( $name ); $this->_templates[$name]['parsed'] = false; $this->_templates[$name]['work'] = ''; $this->_templates[$name]['iteration'] = 0; $this->_templates[$name]['result'] = ''; $this->_vars[$name] = array( 'scalar' => array(), 'rows' => array() ); if (!empty($this->_templates[$name]['defaultVars'])) { foreach ($this->_templates[$name]['defaultVars'] as $varname => $value) { $this->addVar($name, $varname, $value); } } /** * clear child templates as well */ if( $recursive === true ) { $deps = $this->_getDependencies( $name ); foreach( $deps as $dep ) { $this->clearTemplate( $dep, true ); } } return true; } /** * clears all templates * * @access public * @uses clearTemplate() */ function clearAllTemplates() { $templates = array_keys( $this->_templates ); $cnt = count( $templates ); for( $i = 0; $i < $cnt; $i++ ) { $this->clearTemplate( $templates[$i] ); } return true; } /** * frees a template * * All memory consumed by the template * will be freed. * * @access public * @param string name of the template * @param boolean clear dependencies of the template * @see freeAllTemplates() */ function freeTemplate( $name, $recursive = false ) { $name = strtolower( $name ); $key = array_search( $name, $this->_templateList ); if( $key === false ) { return patErrorManager::raiseWarning( PATTEMPLATE_WARNING_NO_TEMPLATE, "Template '$name' does not exist." ); } unset( $this->_templateList[$key] ); $this->_templateList = array_values( $this->_templateList ); /** * free child templates as well */ if( $recursive === true ) { $deps = $this->_getDependencies( $name ); foreach( $deps as $dep ) { $this->freeTemplate( $dep, true ); } } unset( $this->_templates[$name] ); unset( $this->_vars[$name] ); if (isset($this->_discoveredPlaceholders[$name])) { unset($this->_discoveredPlaceholders[$name]); } return true; } /** * frees all templates * * All memory consumed by the templates * will be freed. * * @access public * @see freeTemplate() */ function freeAllTemplates() { $this->_templates = array(); $this->_vars = array(); $this->_templateList = array(); } /** * get _all_ dependencies of a template, * regardless of the subtemplates * * @access private * @param string template name * @return array list of all subtemplates */ function _getDependencies( $template ) { $deps = array(); if( isset( $this->_templates[$template]['dependencies'] ) ) $deps = $this->_templates[$template]['dependencies']; if( isset( $this->_templates[$template]['subtemplates'] ) ) { foreach( $this->_templates[$template]['subtemplates'] as $sub ) { if( isset( $sub['dependencies'] ) ) $deps = array_merge( $deps, $sub['dependencies'] ); } } $deps = array_unique( $deps ); return $deps; } /** * Displays useful information about all or named templates * * This method breaks BC, as it now awaits an array instead of * unlimited parameters. * * @param mixed array of templates that should be dumped, or null if you * want all templates to be dumped * @param string dumper * @access public */ function dump( $restrict = null, $dumper = 'Html' ) { if( is_string( $restrict ) ) $restrict = array( $restrict ); $dumper = &$this->loadModule( 'Dump', $dumper ); if( patErrorManager::isError( $dumper ) ) { return $dumper; } if( is_null( $r
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -