📄 flexy.php
字号:
// Compile the template in $file. require_once 'HTML/Template/Flexy/Compiler.php'; $compiler = HTML_Template_Flexy_Compiler::factory($this->options); $ret = $compiler->compile($this); if (is_a($ret,'PEAR_Error')) { return $this->raiseError('HTML_Template_Flexy fatal error:' .$ret->message, $ret->code, HTML_TEMPLATE_FLEXY_ERROR_DIE); } return $ret; //return $this->$method(); } /** * compiles all templates * Used for offline batch compilation (eg. if your server doesn't have write access to the filesystem). * * @access public * @author Alan Knowles <alan@akbkhome.com> * */ function compileAll($dir = '',$regex='/.html$/') { require_once 'HTML/Template/Flexy/Compiler.php'; $c = new HTML_Template_Flexy_Compiler; $c->compileAll($this,$dir,$regex); } /** * Outputs an object as $t * * for example the using simpletags the object's variable $t->test * would map to {test} * * @version 01/12/14 * @access public * @author Alan Knowles * @param object to output * @param array HTML_Template_Flexy_Elements (or any object that implements toHtml()) * @return none */ function outputObject(&$t,$elements=array()) { if (!is_array($elements)) { return $this->raiseError( 'second Argument to HTML_Template_Flexy::outputObject() was an '.gettype($elements) . ', not an array', HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS ,HTML_TEMPLATE_FLEXY_ERROR_DIE); } if (@$this->options['debug']) { echo "output $this->compiledTemplate<BR>"; } // this may disappear later it's a Backwards Compatibility fudge to try // and deal with the first stupid design decision to not use a second argument // to the method. if (count($this->elements) && !count($elements)) { $elements = $this->elements; } // end depreciated code $this->elements = $this->getElements(); // Overlay values from $elements to $this->elements (which is created from the template) // Remove keys with no corresponding value. foreach($elements as $k=>$v) { // Remove key-value pair from $this->elements if hasn't a value in $elements. if (!$v) { unset($this->elements[$k]); } // Add key-value pair to $this->$elements if it's not there already. if (!isset($this->elements[$k])) { $this->elements[$k] = $v; continue; } // Call the clever element merger - that understands form values and // how to display them... $this->elements[$k] = $this->mergeElement($this->elements[$k] ,$v); } //echo '<PRE>'; print_r(array($elements,$this->elements)); // we use PHP's error handler to hide errors in the template. // use $options['strict'] - if you want to force declaration of // all variables in the template $_error_reporting = false; if (!$this->options['strict']) { $_error_reporting = error_reporting(E_ALL ^ E_NOTICE); } if (!is_readable($this->compiledTemplate)) { return $this->raiseError( "Could not open the template: <b>'{$this->compiledTemplate}'</b><BR>". "Please check the file permissions on the directory and file ", HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // are we using the assign api! if (isset($this->assign)) { if (!$t) { $t = (object) $this->assign->variables; } extract($this->assign->variables); foreach(array_keys($this->assign->references) as $_k) { $$_k = &$this->assign->references[$_k]; } } include($this->compiledTemplate); // Return the error handler to its previous state. if ($_error_reporting !== false) { error_reporting($_error_reporting); } } /** * Outputs an object as $t, buffers the result and returns it. * * See outputObject($t) for more details. * * @version 01/12/14 * @access public * @author Alan Knowles * @param object object to output as $t * @return string - result */ function bufferedOutputObject(&$t,$elements=array()) { ob_start(); $this->outputObject($t,$elements); $data = ob_get_contents(); ob_end_clean(); return $data; } /** * static version which does new, compile and output all in one go. * * See outputObject($t) for more details. * * @version 01/12/14 * @access public * @author Alan Knowles * @param object object to output as $t * @param filename of template * @return string - result */ function &staticQuickTemplate($file,&$t) { $template = new HTML_Template_Flexy; $template->compile($file); $template->outputObject($t); } /** * if debugging is on, print the debug info to the screen * * @access public * @author Alan Knowles <alan@akbkhome.com> * @param string $string output to display * @return none */ function debug($string) { if (is_a($this,'HTML_Template_Flexy')) { if (!$this->options['debug']) { return; } } else if (!@$GLOBALS['_HTML_TEMPLATE_FLEXY']['debug']) { return; } echo "<PRE><B>FLEXY DEBUG:</B> $string</PRE>"; } /** * A general Utility method that merges HTML_Template_Flexy_Elements * Static method - no native debug avaiable.. * * @param HTML_Template_Flexy_Element $original (eg. from getElements()) * @param HTML_Template_Flexy_Element $new (with data to replace/merge) * @return HTML_Template_Flexy_Element the combined/merged data. * @static * @access public */ function mergeElement($original,$new) { // no original - return new if (!$original) { return $new; } // no new - return original if (!$new) { return $original; } // If the properties of $original differ from those of $new and // they are set on $new, set them to $new's. Otherwise leave them // as they are. if ($new->tag && ($new->tag != $original->tag)) { $original->tag = $new->tag; } if ($new->override !== false) { $original->override = $new->override; } if (count($new->children)) { //echo "<PRE> COPY CHILDREN"; print_r($from->children); $original->children = $new->children; } if (is_array($new->attributes)) { foreach ($new->attributes as $key => $value) { $original->attributes[$key] = $value; } } // originals never have prefixes or suffixes.. $original->prefix = $new->prefix; $original->suffix = $new->suffix; if ($new->value !== null) { $original->setValue($new->value); } return $original; } /** * Get an array of elements from the template * * All <form> elements (eg. <input><textarea) etc.) and anything marked as * dynamic (eg. flexy:dynamic="yes") are converted in to elements * (simliar to XML_Tree_Node) * you can use this to build the default $elements array that is used by * outputObject() - or just create them and they will be overlayed when you * run outputObject() * * * @return array of HTML_Template_Flexy_Element sDescription * @access public */ function getElements() { if ($this->elementsFile && file_exists($this->elementsFile)) { require_once 'HTML/Template/Flexy/Element.php'; return unserialize(file_get_contents($this->elementsFile)); } return array(); } /** * Lazy loading of PEAR, and the error handler.. * This should load HTML_Template_Flexy_Error really.. * * @param string message * @param int error type. * @param int an equivalant to pear error return|die etc. * * @return object pear error. * @access public */ function raiseError($message, $type = null, $fatal = HTML_TEMPLATE_FLEXY_ERROR_RETURN ) { HTML_Template_Flexy::debug("<B>HTML_Template_Flexy::raiseError</B>$message"); require_once 'PEAR.php'; if (is_a($this,'HTML_Template_Flexy') && ($fatal == HTML_TEMPLATE_FLEXY_ERROR_DIE)) { // rewrite DIE! return PEAR::raiseError($message, $type, $this->options['fatalError']); } if (isset($GLOBALS['_HTML_TEMPLATE_FLEXY']['fatalError']) && ($fatal == HTML_TEMPLATE_FLEXY_ERROR_DIE)) { return PEAR::raiseError($message, $type,$GLOBALS['_HTML_TEMPLATE_FLEXY']['fatalError']); } return PEAR::raiseError($message, $type, $fatal); } /** * * Assign API - * * read the docs on HTML_Template_Flexy_Assign::assign() * * @param varargs .... * * * @return mixed PEAR_Error or true? * @access public * @see HTML_Template_Flexy_Assign::assign() * @status alpha */ function setData() { require_once 'HTML/Template/Flexy/Assign.php'; // load assigner.. if (!isset($this->assign)) { $this->assign = new HTML_Template_Flexy_Assign; } return $this->assign->assign(func_get_args()); } /** * * Assign API - by Reference * * read the docs on HTML_Template_Flexy_Assign::assign() * * @param key string * @param value mixed * * @return mixed PEAR_Error or true? * @access public * @see HTML_Template_Flexy_Assign::assign() * @status alpha */ function setDataByRef($k,&$v) { require_once 'HTML/Template/Flexy/Assign.php'; // load assigner.. if (!isset($this->assign)) { $this->assign = new HTML_Template_Flexy_Assign; } $this->assign->assignRef($k,$v); } /** * * Plugin (used by templates as $this->plugin(...) or {this.plugin(#...#,#....#)} * * read the docs on HTML_Template_Flexy_Plugin() * * @param varargs .... * * @return mixed PEAR_Error or true? * @access public * @see HTML_Template_Flexy_Plugin * @status alpha */ function plugin() { require_once 'HTML/Template/Flexy/Plugin.php'; // load pluginManager. if (!isset($this->plugin)) { $this->plugin = new HTML_Template_Flexy_Plugin; $this->plugin->flexy = &$this; } return $this->plugin->call(func_get_args()); } /** * * output / display ? - outputs an object, without copy by references.. * * @param optional mixed object to output * * @return mixed PEAR_Error or true? * @access public * @see HTML_Template_Flexy::ouptutObject * @status alpha */ function output($object = false) { return $this->outputObject($object); } /** * * render the template with data.. * * @param optional mixed object to output * * @return mixed PEAR_Error or true? * @access public * @see HTML_Template_Flexy::ouptutObject * @status alpha */ function toString($object = false) { return $this->bufferedOutputObject($object); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -