cvs: pear /Template Template.php
| From: | Alan Knowles | Date: | Sun, 13 Jun 2004 14:08:10 +0000 |
| Subject: | cvs: pear /Template Template.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-20594@lists.php.net to get a copy of this message | ||
alan_k Sun Jun 13 10:08:10 2004 EDT
Added files:
/pear/Template Template.php
Log:
the start of something mindblowing.. - or........
http://cvs.php.net/co.php/pear/Template/Template.php?r=1.1&p=1 Index: pear/Template/Template.php +++ pear/Template/Template.php <?php /* ------------------------------ the dumbest usage a php code template..: $x = new Template(array( 'template_dir'=>dirname(__FILE__) )); $x->open('mytemplate.php'); $x->setData('xxx','yyy'); $x->output(); ------------------------------ dumbest usage Flexy/Tokenizer Mode: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->outputData($this); ------------------------------ mix and match: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->setData('xxx','yyy'); $x->output(); ------------------------------ flexy with element object.: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->outputData(array('object'=>&$this, 'elements'=>$elements))); ------------------------------ WACT with element object.: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); //??? need some thought.. $x->data['elements'] = $x->getElement('formentry'); $x->data['elements']->setSomething('xxxx'); $x->outputData(array('object'=>&$this, 'elements'=>$elements))); ------------------------------ IT/Sigma type $x = new Template(array( 'template_dir' =>dirname(__FILE__) 'complier' =>'block', 'block.caching' => true, 'compile_dir' => '/tmp/templateCache', ); $x->setData('xxx','yyy'); $x->block('set','block1'); $x->setData('xxx','yyy'); $x->setData('xxx','yyy'); $x->block('parse'); $x->block('file', array('new_block'=>'example_3.html')); foreach ($something as $a=>$b) { $x->setData('xxx','yyy'); $x->block('parse','new_block') } $x->output(); ------------------------------------------------------------------------------------------- The Theory goes the options array that you use with the constructor could have 100's of potential options. -- less than 6 defaults should be essentail for it's normal operation -- For most common usage: between 1 - 3 options are set in the constructor... Other than the standard ones in the engine.. This should list the all the options, along with where to find descriptions of them. 'locale' @see Template_MultiSource / Template_Compiler_* 'source.allow_multiple' @see Template_MultiSource 'source.sort_order' 'source.type' 'compiler.folder' @see Template_Compiler 'compiler.force' 'compiler.regex.filters' @see Template_Compiler_Regex 'compiler.standard.non_HTML' @see Template_Compiler_Flexy 'compiler.standard.allow_PHP' 'compiler.standard.element_parsing' 'compiler.standard.compile_to_String' 'compiler.standard.privates' 'compiler.standard.globals' 'compiler.standard.globalfunctions' 'compiler.standard.translation2' 'plugins.date_format' 'plugins.img_rewrite' */ // ERRORS: define('TEMPLATE_ERROR_SYNTAX', -1); // syntax error in template. define('TEMPLATE_ERROR_INVALIDARGS', -2); // bad arguments to methods. define('TEMPLATE_ERROR_FILE', -3); // file access problem define('TEMPLATE_ERROR_RETURN', 1); // RETURN ERRORS define('TEMPLATE_ERROR_DIE', 8); // FATAL DEATH /** * Universal Template Provider * * Example to be added.. * */ class Template { /* * @var array $options the options for initializing the template class */ var $options = array( // where the template comes from. ------------------------------------------ 'template_dir' => '', // where are your templates = can be an array, or // a list seperatated by the system PATH_SEPERATOR 'compiler' => '', // which compiler to use. // Flexy, Regex, '' (Raw), Xipe, // Smarty, WACT, Block (ITX/Sigma/phplib) 'debug' => false, // prints a few messages 'output.strict' => false, // All elements in the template must be defined - // makes php E_NOTICE warnings appear when outputing template. 'errors.behaviour' => TEMPLATE_ERROR_DIE, // default behavior is to die on errors in template. // 'plugins' => array(), // load classes to be made available via the plugin method // eg. = array('Savant') - loads the Savant methods. // = array('MyClass_Plugins' => 'MyClass/Plugins.php') // Class, and where to include it from.. ); /** * The compiled template filename (Full path) * * @var string * @access public */ var $files = array( 'currentTemplate' => '', 'gettextStringsFile' => '', 'elementsFile' => '', ); /** * Array of various data stored in engine. * * @var array of * @access private */ var $data = array( 'variables' => array(), 'byref' => array(), 'elements' => array(), 'object' => false, 'elements_original' => false, ); /** * Array of feature providers * * @var array of Template_* * @access private */ var $provider = array( 'Assign' => null, // assign API 'Plugin' => null, // plugins 'Block' => null, // IT type templates 'Multisource' => null, // where is the file? ); /** * Constructor * * Initializes the Template engine, and sets up behaviour based on the provided options * * @access public * @param array $options (Optional) */ function Template( $options=array() ) { $this->options = array_merge($this->options, $options); // option manipulation goes here! - dont do to much of it... // the providers should do their own option manipulation if necessary when they // are loaded.. $this->options['template_dir'] = is_string($this->options['template_dir']) ? explode(PATH_SEPARATOR,$this->options['template_dir'] ) : $this->options['template_dir']; } /** * open (and optionally parse or compile a template ) * * @access public * @param string $file relative to the 'template_dir' which you set when calling the constructor * @return boolean true on success. (or string, if compiler.toString) PEAR_Error on failure.. */ function open( $file ) { if (!$file) { return $this->raiseError('Template::compile no file selected', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } // If we are using any of the fancy path stuff. - let the multisource provider work // out where the template is.. if (($this->options['locale'] !== false) || count($this->options['template_dir']) > 1) { $this->assertProvider('Multisource'); $this->provider['Multisource']->find($file); } else { $this->files['currentTemplate'] = $this->options['template_dir'][0] . PATH_SEPERATOR . $file; if (!file_exists($this->files['currentTemplate'])) { $this->files['currentTemplate'] = false; } } // cant find the template!!! if ($this->files['currentTemplate'] === false) { // check if the compile dir has been created return $this->raiseError( "Could not find Template {$file} in any of the directories<br>" . implode("<BR>",$this->options['template_dir']) , HTML_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } // What type of compiler - do we need to do anything.. switch($this->options['compiler']) { case '': $this->files['compiledTemplate'] = $this->files['currentTemplate']; $this->debug("Using Raw Compiler"); return true; case 'Block': return $this->block('load'); default: // some kind of compiler specified... $this->assertProvider('Compiler'); if (true !=== ($r = $this->provider['Complier']->needsCompiling())) { return $r; } return $this->provider['Complier']->compile(); } } /** * Outputs the template.. * * @access public * @return none */ function output() { $this->debug( "output {$this->files['compliedTemplate']}"); if ($this->options['compiler'] == 'block') { echo $this->block('get'); return; } // some compilers will set element merge // takes what was in the template and overlays it with // the contents of $this->data['elements'] // this may end up being moved into the head code // of the compiled template (just like the $t mapping.. if (isset($this->options['output.element_merge'])) { // loads elements into data['elementsOriginal'] if ($this->getElement()) { // elements' loaded preumably... as we have some! HTML_Template_Element::mergeAllElements($this); } } // 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); } // either extract, or put it all in $data['object'] // the engine can generate $t = &$this->data['object'] at the top anyway.. if (isset($this->options['no_extract'])) { if (!$this->data['object']) { $this->data['object'] = (object) $this->data['variables']; foreach(array_keys($this->data['byref']) as $k) $this->data['object']->$k = $this->$this->data['byref'][$k]; } } } else { // question what should extract style template do with object // and no vars?? // foreach(array_keys($this->output['object']) as $_k) { // $$_k = $this->output['object']->$_k; // } extract($this->data['variables']); foreach(array_keys($this->data['byref']) as $_k) { $$_k = &$this->$this->data['byref'][$_k]; } } if (isset($this->data['template_as_string'])) { eval($this->data['template_as_string']); } else { include $this->files['compiledTemplate']; } // Return the error handler to its previous state. if ($_error_reporting !== false) { error_reporting($_error_reporting); } } /** * get the result of the template overlayed with data as a string. * * @access public * @param object object to output as $t * @return string - result */ function toString() { { ob_start(); if (is_a($r = $this->output(), 'PEAR_Error'))) { ob_end_clean(); return $r; } $data = ob_get_contents(); ob_end_clean(); return $data; } /** * if debugging is on, print the debug info to the screen * * @access public * @param string $string output to display * @return none */ function debug($string) { if (!$this->options['debug']) { return; } if (ini_get('html_errors')) { echo "<PRE><B>TEMPLATE DEBUG:</B> $string</PRE>"; } else { echo "TEMPLATE DEBUG: $string\n"; } } /** * Lazy loading of PEAR, and the error handler.. * This should load Templae_Error really.. (where all the mapping of error stuff can go..) * * @param string message * @param int error type. * @param int an equivalant to pear error return|die etc. * @param array args for printf view of message... * * @return object pear error. * @access public */ function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN ,$args = array()) { $this->debug("<B>TEMPLATE::raiseError</B>$message"); $this->assertProvider('Error'); return $this->provider['Error']->raiseError($message,$type,$severity,$args); } /** * * Assign Variables to the template * * read the docs on TEMPLATE_Assign::setData() * * @param varargs .... * * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Assign::setData() * @status alpha */ function setData() { $this->assertProvider('Assign') return $this->provider['Assign']>setData(func_get_args()); } /** * * Assign Variables to the template by Reference * normally usefull if you are going to use methods on them.. * * read the docs on TEMPLATE_Assign::setDataByRef() * * @param key string * @param value mixed * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Assign::setDataByRef() */ function setDataByRef($k,&$v) { // load assigner.. $this->assertProvider('Assign') return $this->provider['Assign']->setDataByRef($k,$v); } /** * * reset __All__ the data.. * * * @return none * @access public */ function clearData() { $x = new Template; $this->data = $x->data; } /** * * Plugin (used by templates as $this->plugin(...) or {this.plugin(#...#,#....#)} * * read the docs on Template_Plugin() * * @param varargs .... * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Plugin * @status alpha */ function plugin() { // load pluginManager. $this->assertProvider('Plugin'); return $this->provider['Plugin']->call(func_get_args()); } /** * Block API support * * An idea to support block regex/str_replace tempaltes * useing the idea * $template->block('set','.....'); // set current block * $template->block('get','.....'); // get the parsed results of a block??? * $template->block('touch','.....'); // touch block * $template->block('hide','.....'); // hide block * $template->block('add','.....'); // add a block * $template->block('addFile','.....'); // add a block from a file? * $template->block('exists','.....'); // does a block exist? * $template->block('list','.....'); // list the blocks * $template->block('show','.....'); // output? a block * * @param string action * @param varargs ... * * @return mixed * @access public */ function block($name) { $this->assertProvider('Block'); $args = func_get_args(); array_shift($args); return $this->providers['Block']->$name($args); } /** * Assert that a provider is loaded, Load if it isnt.. * * 'Providers' provide the features that can be used by the varous engines * This is how features are added without bloating the core too much.. * * This can be used by any of the backends to load classes, and instance them in * the provider array (eg. $template->assignProvider('Compiler/Flexy');) * * @param string Name of feature.. * * @return none * @access public */ function assertProvider($name) { if (isset($this->providers[$name])) { return; } require_once "Template/$name.php"; $class = 'Template_'.str_replace('/','_',$name); // this may not work correctly - constructors can be nasty // when passing by reference.. $this->provider[$name] = &call_user_func(array($class,'construct'),$this); } // ============================================================================================= // ------------------------------------------ Short Cuts ----------------------------------- /** * Sets Data in the store directly (bypassing assign API) * Thens outputs. * * * examples : * $engine->outputData(&$this); * * $engine->outputData( * 'object' => &$this * 'elements' => $ellements * * provides the lightweight output without assigment that outputObject used to do.. * * @access public * @return none */ function outputData(&$data) { // options : object: - switch(get_type($data)) { case 'object': $this->data['object'] = &$data; return $this->output(); case 'array': $this->data = &$data; return $this->output(); default: $this->output(); // mmh what about other types!!!??? } } // ============================================================================================= // Template Specific Code anything below here is dependant on it being supported in the backend: // At present this includes: // elements (eg. XML_Tree like elements coming from HTML (WACT/Flexy? others?) // this whole area still needs a bit more thought... /** * 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 TEMPLATE_Element s * @access public */ function getElement($name=false) { // load the elements if required: if (!isset($this->data['elements_original']) && $this->files['elementsFile'] &&file_exists($this->files['elementsFile']) ) { // maybe more comlexity required? require_once 'HTML/Template/Element.php'; $this->data['elements_original'] = unserialize(file_get_contents($this->files['elementsFile'])); } if ($name === false) { return $this->data['elements_original']; } return isset($this->data['elements_original'][$name]) ? $this->data['elements_original'][$name] : false; } }
http://cvs.php.net/co.php/pear/Template/Template.php?r=1.1&p=1 Index: pear/Template/Template.php +++ pear/Template/Template.php <?php /* ------------------------------ the dumbest usage a php code template..: $x = new Template(array( 'template_dir'=>dirname(__FILE__) )); $x->open('mytemplate.php'); $x->setData('xxx','yyy'); $x->output(); ------------------------------ dumbest usage Flexy/Tokenizer Mode: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->outputData($this); ------------------------------ mix and match: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->setData('xxx','yyy'); $x->output(); ------------------------------ flexy with element object.: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); $x->open('mytemplate.html'); $x->outputData(array('object'=>&$this, 'elements'=>$elements))); ------------------------------ WACT with element object.: $x = new Template(array( 'template_dir'=>dirname(__FILE__) 'complier'=>'Standard', ); //??? need some thought.. $x->data['elements'] = $x->getElement('formentry'); $x->data['elements']->setSomething('xxxx'); $x->outputData(array('object'=>&$this, 'elements'=>$elements))); ------------------------------ IT/Sigma type $x = new Template(array( 'template_dir' =>dirname(__FILE__) 'complier' =>'block', 'block.caching' => true, 'compile_dir' => '/tmp/templateCache', ); $x->setData('xxx','yyy'); $x->block('set','block1'); $x->setData('xxx','yyy'); $x->setData('xxx','yyy'); $x->block('parse'); $x->block('file', array('new_block'=>'example_3.html')); foreach ($something as $a=>$b) { $x->setData('xxx','yyy'); $x->block('parse','new_block') } $x->output(); ------------------------------------------------------------------------------------------- The Theory goes the options array that you use with the constructor could have 100's of potential options. -- less than 6 defaults should be essentail for it's normal operation -- For most common usage: between 1 - 3 options are set in the constructor... Other than the standard ones in the engine.. This should list the all the options, along with where to find descriptions of them. 'locale' @see Template_MultiSource / Template_Compiler_* 'source.allow_multiple' @see Template_MultiSource 'source.sort_order' 'source.type' 'compiler.folder' @see Template_Compiler 'compiler.force' 'compiler.regex.filters' @see Template_Compiler_Regex 'compiler.standard.non_HTML' @see Template_Compiler_Flexy 'compiler.standard.allow_PHP' 'compiler.standard.element_parsing' 'compiler.standard.compile_to_String' 'compiler.standard.privates' 'compiler.standard.globals' 'compiler.standard.globalfunctions' 'compiler.standard.translation2' 'plugins.date_format' 'plugins.img_rewrite' */ // ERRORS: define('TEMPLATE_ERROR_SYNTAX', -1); // syntax error in template. define('TEMPLATE_ERROR_INVALIDARGS', -2); // bad arguments to methods. define('TEMPLATE_ERROR_FILE', -3); // file access problem define('TEMPLATE_ERROR_RETURN', 1); // RETURN ERRORS define('TEMPLATE_ERROR_DIE', 8); // FATAL DEATH /** * Universal Template Provider * * Example to be added.. * */ class Template { /* * @var array $options the options for initializing the template class */ var $options = array( // where the template comes from. ------------------------------------------ 'template_dir' => '', // where are your templates = can be an array, or // a list seperatated by the system PATH_SEPERATOR 'compiler' => '', // which compiler to use. // Flexy, Regex, '' (Raw), Xipe, // Smarty, WACT, Block (ITX/Sigma/phplib) 'debug' => false, // prints a few messages 'output.strict' => false, // All elements in the template must be defined - // makes php E_NOTICE warnings appear when outputing template. 'errors.behaviour' => TEMPLATE_ERROR_DIE, // default behavior is to die on errors in template. // 'plugins' => array(), // load classes to be made available via the plugin method // eg. = array('Savant') - loads the Savant methods. // = array('MyClass_Plugins' => 'MyClass/Plugins.php') // Class, and where to include it from.. ); /** * The compiled template filename (Full path) * * @var string * @access public */ var $files = array( 'currentTemplate' => '', 'gettextStringsFile' => '', 'elementsFile' => '', ); /** * Array of various data stored in engine. * * @var array of * @access private */ var $data = array( 'variables' => array(), 'byref' => array(), 'elements' => array(), 'object' => false, 'elements_original' => false, ); /** * Array of feature providers * * @var array of Template_* * @access private */ var $provider = array( 'Assign' => null, // assign API 'Plugin' => null, // plugins 'Block' => null, // IT type templates 'Multisource' => null, // where is the file? ); /** * Constructor * * Initializes the Template engine, and sets up behaviour based on the provided options * * @access public * @param array $options (Optional) */ function Template( $options=array() ) { $this->options = array_merge($this->options, $options); // option manipulation goes here! - dont do to much of it... // the providers should do their own option manipulation if necessary when they // are loaded.. $this->options['template_dir'] = is_string($this->options['template_dir']) ? explode(PATH_SEPARATOR,$this->options['template_dir'] ) : $this->options['template_dir']; } /** * open (and optionally parse or compile a template ) * * @access public * @param string $file relative to the 'template_dir' which you set when calling the constructor * @return boolean true on success. (or string, if compiler.toString) PEAR_Error on failure.. */ function open( $file ) { if (!$file) { return $this->raiseError('Template::compile no file selected', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } // If we are using any of the fancy path stuff. - let the multisource provider work // out where the template is.. if (($this->options['locale'] !== false) || count($this->options['template_dir']) > 1) { $this->assertProvider('Multisource'); $this->provider['Multisource']->find($file); } else { $this->files['currentTemplate'] = $this->options['template_dir'][0] . PATH_SEPERATOR . $file; if (!file_exists($this->files['currentTemplate'])) { $this->files['currentTemplate'] = false; } } // cant find the template!!! if ($this->files['currentTemplate'] === false) { // check if the compile dir has been created return $this->raiseError( "Could not find Template {$file} in any of the directories<br>" . implode("<BR>",$this->options['template_dir']) , HTML_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } // What type of compiler - do we need to do anything.. switch($this->options['compiler']) { case '': $this->files['compiledTemplate'] = $this->files['currentTemplate']; $this->debug("Using Raw Compiler"); return true; case 'Block': return $this->block('load'); default: // some kind of compiler specified... $this->assertProvider('Compiler'); if (true !=== ($r = $this->provider['Complier']->needsCompiling())) { return $r; } return $this->provider['Complier']->compile(); } } /** * Outputs the template.. * * @access public * @return none */ function output() { $this->debug( "output {$this->files['compliedTemplate']}"); if ($this->options['compiler'] == 'block') { echo $this->block('get'); return; } // some compilers will set element merge // takes what was in the template and overlays it with // the contents of $this->data['elements'] // this may end up being moved into the head code // of the compiled template (just like the $t mapping.. if (isset($this->options['output.element_merge'])) { // loads elements into data['elementsOriginal'] if ($this->getElement()) { // elements' loaded preumably... as we have some! HTML_Template_Element::mergeAllElements($this); } } // 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); } // either extract, or put it all in $data['object'] // the engine can generate $t = &$this->data['object'] at the top anyway.. if (isset($this->options['no_extract'])) { if (!$this->data['object']) { $this->data['object'] = (object) $this->data['variables']; foreach(array_keys($this->data['byref']) as $k) $this->data['object']->$k = $this->$this->data['byref'][$k]; } } } else { // question what should extract style template do with object // and no vars?? // foreach(array_keys($this->output['object']) as $_k) { // $$_k = $this->output['object']->$_k; // } extract($this->data['variables']); foreach(array_keys($this->data['byref']) as $_k) { $$_k = &$this->$this->data['byref'][$_k]; } } if (isset($this->data['template_as_string'])) { eval($this->data['template_as_string']); } else { include $this->files['compiledTemplate']; } // Return the error handler to its previous state. if ($_error_reporting !== false) { error_reporting($_error_reporting); } } /** * get the result of the template overlayed with data as a string. * * @access public * @param object object to output as $t * @return string - result */ function toString() { { ob_start(); if (is_a($r = $this->output(), 'PEAR_Error'))) { ob_end_clean(); return $r; } $data = ob_get_contents(); ob_end_clean(); return $data; } /** * if debugging is on, print the debug info to the screen * * @access public * @param string $string output to display * @return none */ function debug($string) { if (!$this->options['debug']) { return; } if (ini_get('html_errors')) { echo "<PRE><B>TEMPLATE DEBUG:</B> $string</PRE>"; } else { echo "TEMPLATE DEBUG: $string\n"; } } /** * Lazy loading of PEAR, and the error handler.. * This should load Templae_Error really.. (where all the mapping of error stuff can go..) * * @param string message * @param int error type. * @param int an equivalant to pear error return|die etc. * @param array args for printf view of message... * * @return object pear error. * @access public */ function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN ,$args = array()) { $this->debug("<B>TEMPLATE::raiseError</B>$message"); $this->assertProvider('Error'); return $this->provider['Error']->raiseError($message,$type,$severity,$args); } /** * * Assign Variables to the template * * read the docs on TEMPLATE_Assign::setData() * * @param varargs .... * * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Assign::setData() * @status alpha */ function setData() { $this->assertProvider('Assign') return $this->provider['Assign']>setData(func_get_args()); } /** * * Assign Variables to the template by Reference * normally usefull if you are going to use methods on them.. * * read the docs on TEMPLATE_Assign::setDataByRef() * * @param key string * @param value mixed * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Assign::setDataByRef() */ function setDataByRef($k,&$v) { // load assigner.. $this->assertProvider('Assign') return $this->provider['Assign']->setDataByRef($k,$v); } /** * * reset __All__ the data.. * * * @return none * @access public */ function clearData() { $x = new Template; $this->data = $x->data; } /** * * Plugin (used by templates as $this->plugin(...) or {this.plugin(#...#,#....#)} * * read the docs on Template_Plugin() * * @param varargs .... * * @return mixed PEAR_Error or true? * @access public * @see TEMPLATE_Plugin * @status alpha */ function plugin() { // load pluginManager. $this->assertProvider('Plugin'); return $this->provider['Plugin']->call(func_get_args()); } /** * Block API support * * An idea to support block regex/str_replace tempaltes * useing the idea * $template->block('set','.....'); // set current block * $template->block('get','.....'); // get the parsed results of a block??? * $template->block('touch','.....'); // touch block * $template->block('hide','.....'); // hide block * $template->block('add','.....'); // add a block * $template->block('addFile','.....'); // add a block from a file? * $template->block('exists','.....'); // does a block exist? * $template->block('list','.....'); // list the blocks * $template->block('show','.....'); // output? a block * * @param string action * @param varargs ... * * @return mixed * @access public */ function block($name) { $this->assertProvider('Block'); $args = func_get_args(); array_shift($args); return $this->providers['Block']->$name($args); } /** * Assert that a provider is loaded, Load if it isnt.. * * 'Providers' provide the features that can be used by the varous engines * This is how features are added without bloating the core too much.. * * This can be used by any of the backends to load classes, and instance them in * the provider array (eg. $template->assignProvider('Compiler/Flexy');) * * @param string Name of feature.. * * @return none * @access public */ function assertProvider($name) { if (isset($this->providers[$name])) { return; } require_once "Template/$name.php"; $class = 'Template_'.str_replace('/','_',$name); // this may not work correctly - constructors can be nasty // when passing by reference.. $this->provider[$name] = &call_user_func(array($class,'construct'),$this); } // ============================================================================================= // ------------------------------------------ Short Cuts ----------------------------------- /** * Sets Data in the store directly (bypassing assign API) * Thens outputs. * * * examples : * $engine->outputData(&$this); * * $engine->outputData( * 'object' => &$this * 'elements' => $ellements * * provides the lightweight output without assigment that outputObject used to do.. * * @access public * @return none */ function outputData(&$data) { // options : object: - switch(get_type($data)) { case 'object': $this->data['object'] = &$data; return $this->output(); case 'array': $this->data = &$data; return $this->output(); default: $this->output(); // mmh what about other types!!!??? } } // ============================================================================================= // Template Specific Code anything below here is dependant on it being supported in the backend: // At present this includes: // elements (eg. XML_Tree like elements coming from HTML (WACT/Flexy? others?) // this whole area still needs a bit more thought... /** * 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 TEMPLATE_Element s * @access public */ function getElement($name=false) { // load the elements if required: if (!isset($this->data['elements_original']) && $this->files['elementsFile'] &&file_exists($this->files['elementsFile']) ) { // maybe more comlexity required? require_once 'HTML/Template/Element.php'; $this->data['elements_original'] = unserialize(file_get_contents($this->files['elementsFile'])); } if ($name === false) { return $this->data['elements_original']; } return isset($this->data['elements_original'][$name]) ? $this->data['elements_original'][$name] : false; } }