cvs: pear /Template Template.php

From: Date: Wed, 16 Jun 2004 19:51:24 +0000
Subject: cvs: pear /Template Template.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20762@lists.php.net to get a copy of this message
jausions Wed Jun 16 15:51:24 2004 EDT Modified files: /pear/Template Template.php Log: - Typo mixes, - Added comments. - Fixed open() to return TRUE when needsCompiling() returns FALSE - Added loading Source provider on template location containing "://" - Use DIRECTORY_SEPARATOR instead of PATH_SEPARATOR when build path to template in open() - Tweaking assertProvider() to use proper class

http://cvs.php.net/diff.php/pear/Template/Template.php?r1=1.13&r2=1.14&ty=u Index: pear/Template/Template.php diff -u pear/Template/Template.php:1.13 pear/Template/Template.php:1.14 --- pear/Template/Template.php:1.13 Wed Jun 16 11:58:00 2004 +++ pear/Template/Template.php Wed Jun 16 15:51:24 2004 @@ -41,29 +41,29 @@ define('TEMPLATE_ERROR_DIE', 8); - + /** -* +* * This class implements a "universal" template provider for PEAR. * * See example usage and in-progress documentation at {@link * http://wiki.ciaweb.net/yawiki/index.php?area=Template} -* -* $Id: Template.php,v 1.13 2004/06/16 15:58:00 alan_k Exp $ -* +* +* $Id: Template.php,v 1.14 2004/06/16 19:51:24 jausions Exp $ +* * @package Template * * @author Philippe Jausions <Philippe.Jausions@11abacus.com> -* +* * @author Alan Knowles <alan@akbkhome.com> -* +* * @author Paul M. Jones <pmjones@ciaweb.net> -* +* */ class Template { - - + + /** * The options for initializing the template class. * @@ -96,50 +96,49 @@ * * @access public * @var array - **/ - - + **/ + + var $options = array( 'template.dir' => '', 'compiler' => '', - 'debug' => false, 'debug.level' => 1, 'output.strict' => false, 'errors.behaviour' => TEMPLATE_ERROR_DIE, 'plugins' => array() - ); - - + ); + + /** - * + * * Array of files used by the Template * some may be with full path, others may just be what the user requested * * current_template = full path of the template that is being used. - * requested_template = local path (relative to options['template_dir'] of the template + * requested_template = local path (relative to options['template.dir'] of the template * being used. * compiled_template = file to be output (when using compiled templates) * * @access public - * + * * @var string - * + * */ - + var $files = array( - 'current_template' => '', - 'requested_template' => '', - 'compiled_template' => '', + 'current_template' => '', + 'requested_template' => '', + 'compiled_template' => '', ); - - + + /** - * + * * Array of various data portions stored in engine. * * 'variables' => Data assigned to the template by copy. * 'byref' => Data assigned to the template by reference. - * 'object' => single object with public variables representing data you want + * 'object' => single object with public variables representing data you want * overlayed onto the template * * @@ -147,204 +146,211 @@ * 'template_compiled' => the complied template as a string (when using compiling * templates to a string) * - * 'elements' => array of XML_Tree type elements you want to overlay onto the + * 'elements' => array of XML_Tree type elements you want to overlay onto the * template * 'elements_original' => array of XML_Tree type elements that are in the template - * to be rendered. - * + * to be rendered. + * * Providers have full access to these, * you have read access to them. * write access should be carefully considered.. * * @var array * @access semi-private - * + * */ - + var $data = array( 'variables' => array(), 'byref' => array(), - 'elements' => array(), + 'elements' => array(), 'object' => false, 'elements_original' => false ); - - + + /** - * - * Array of feature providers - * + * + * Array of feature providers + * * @access private - * + * * @var array - * + * */ - + var $provider = array( 'Assign' => null, // assign API 'Plugin' => null, // plugins 'Block' => null, // IT type templates 'Source' => null, // template source locater and loader? 'Compiler' => null // To compile/parse templates - ); - - + ); + + /** * * Constructor. - * + * * Initializes the Template engine, and sets up behaviour based on * the provided options. * this doesnt handle pass by ref setting of things..! - * + * * @access public - * - * @param array $options An array of options to pass to the Template object; + * + * @param array $options An array of options to pass to the Template object; * @see the $options property. - * + * */ - + 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']; + + $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. - * - * TODO : Possible alternatives : use / useTemplate + * + * TODO : Possible alternatives : use / useTemplate * * @access public - * - * @param mixed $file The file path of the template source, relative - * to the 'template_dir' option set in the constructor. + * + * @param mixed $location The file path of the template source, relative + * to the 'template.dir' option set in the constructor. * use array('string'=>$template_string) or $filepointer * for other types of source. - * + * * @return mixed Boolean true on success (or string, if * 'compiler.toString' is set), or PEAR_Error on failure. - * + * */ - + function open( $location ) { - + if (!$location) { - return $this->raiseError('Template::compile no file selected', + return $this->raiseError('Template::open() no template location provided', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } - + // should we have a check before we do this? - // clear the files - + // clear the files + + // A "_resetTemplate()" method could also probably be good too + // if more things need to be reset later on... $x = new Template; $this->files = $x->files; - + // TODO: accept other input: // eg. file handle is_resource(), or string is_string() : // probably want to add another provider for this... - - - // If we are using any of the fancy path stuff or file pointers etc.. - let the Source provider work - // out where the template is.. - - if (($this->options['locale'] !== false) || - (count($this->options['template_dir']) > 1) || - !is_string($location)) + + + // If we are using any of the fancy path stuff or file pointers etc.. - + // let the Source provider work out where the template is.. + + if (($this->options['locale'] !== false) || + (count($this->options['template.dir']) > 1) || + !is_string($location) + || strpos($location, '://') > 0) { $this->assertProvider('Source'); - if (true !== ($r = $this->provider['Multisource']->open($location))) { + if (true !== ($r = $this->provider['Source']->open($location))) { return $r; } } else { - - $this->files['current_template'] = - $this->options['template_dir'][0] . PATH_SEPERATOR . $location; - + + $this->files['current_template'] = + $this->options['template.dir'][0] . DIRECTORY_SEPARATOR . $location; + $this->files['requested_template'] = $location; - + if (!file_exists($this->files['current_template'])) { // cant find the template!!! return $this->raiseError( - "Could not find Template %s in the configured path %s" . - implode(", ",$this->options['template_dir']) , + "Could not find Template %s in the configured path %s" . + implode(", ",$this->options['template.dir']) , TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, - array($location, $this->options['template_dir'][0])); + array($location, $this->options['template.dir'][0])); } - + } - - - - + + + + // What type of compiler - do we need to do anything.. switch($this->options['compiler']) { - + case '': $this->files['compiledTemplate'] = $this->files['current_template']; $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())) { + if (!is_bool($r = $this->provider['Compiler']->needsCompiling())) { return $r; } - return $this->provider['Complier']->compile(); - } - + if ($r === true) { + return $this->provider['Compiler']->compile(); + } + } + + return true; + } - - + + /** - * + * * Outputs the template. * * @access public - * + * * @return void * */ - - function output() + + function output() { - - + + $this->debug( "output {$this->files['compiled_template']}"); - + if (isset($this->options['filters'])) { $this->assertProvider('Filter'); return $this->provider['Filter']->output(); } - - + + if ($this->options['compiler'] == 'block') { echo $this->block('get'); return true; } - - - - + + + + // 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 $this->data['elementsOriginal'] if ($this->getElement()) { @@ -352,22 +358,22 @@ 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']; @@ -381,29 +387,29 @@ // 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['compiled_template']; } - - // Return the error handler to its previous state. - + + // Return the error handler to its previous state. + if ($_error_reporting !== false) { error_reporting($_error_reporting); } return true; } - - + + /** * * Gets the result of the template overlaid with data as a string. @@ -415,24 +421,25 @@ * @return string - result * */ - function toString() + function toString() { if (isset($this->options['filters'])) { $this->assertProvider('Filter'); return $this->provider['Filter']->toString(); } - + ob_start(); if (true !== ($r = $this->output())) { ob_end_clean(); return $r; } + // Considering ob_get_clean() for a boost for very large templates $data = ob_get_contents(); ob_end_clean(); return $data; } - - + + /** * * Prints the debug info to the screen if debugging is on. @@ -440,28 +447,29 @@ * @access public * * @param string $string Debug message to display. - * + * @param integer $level Verbose debug level (The higher the level the more + * detailed the information should be.) * @return void * */ - - function debug($string,$level =1 ) - { + + function debug($string, $level= 1) + { if ($level > $this->options['debug.level'] ) { return; } $this->assertProvider('Debug'); $this->provider['Debug']->debug($string); } - - - + + + /** - * + * * Global RaiseError - very lightweight! - * + * * This may return a PEAR_Error/ErrorStack, or throw an exception! - * + * * @access public * @param string $mesage The error message text. * @param int $type Error type. @@ -469,11 +477,11 @@ * @param array $args Args for printf view of message. * * @return object PEAR_Error object. - * + * */ - + function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN, - $args = array()) + $args = array()) { $this->debug("<B>TEMPLATE::raiseError</B>$message ($type/$severity)".print_r($args,true)); $this->assertProvider('Error'); @@ -483,87 +491,87 @@ /** - * + * * 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 call_user_func_array(array(&$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 + * + * @return none * @access public */ - - function clearData() + + 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 * @@ -578,71 +586,78 @@ * $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 + * @access public */ - + function block($name) { $this->assertProvider('Block'); $args = func_get_args(); array_shift($args); return $this->provider['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');) - * + * + * This can be used by any of the backends to load classes, and instance + * them in the provider array eg: + * <code> + * $template->assignProvider('Compiler/Flexy'); + * </code> + * + * @todo Possibly return a PEAR_Error or trigger_error() on class loading + * issue... * @param string Name of feature.. * * @return true or PEAR::Error. - * @access public + * @access public */ - + function assertProvider($name) { - if (isset($this->providers[$name])) { + if (isset($this->providers[$name])) { return true; } - - $file = "Template/$name.php"; - $class = 'Template_'.str_replace('/','_',$name); + + // Not sure if that belongs to "options" array if (isset($this->options['provider'][$name])) { $file = $this->options['provider'][$name][0]; $class = $this->options['provider'][$name][1]; + } else { + $file = "Template/$name.php"; + $class = 'Template_'.str_replace('/','_',$name); } - require_once "Template/$name.php"; - - // this may not work correctly - constructors can be nasty + require_once $file; + + // this may not work correctly - constructors can be nasty // when passing by reference.. $this->provider[$name] = new $class; return $this->provider[$name]->initProvider($this); } - + // ============================================================================================= // ------------------------------------------ Short Cuts ----------------------------------- - + /** * Sets Data in the store directly (bypassing assign API) * Thens outputs. * * - * examples : + * examples : * $engine->outputData(&$this); * * $engine->outputData( - * 'object' => &$this + * 'object' => &$this * 'elements' => $ellements * * provides the lightweight output without assigment that outputObject used to do.. @@ -650,10 +665,10 @@ * @access public * @return none */ - - function outputData(&$data) + + function outputData(&$data) { - // options : object: - + // options : object: - switch(get_type($data)) { case 'object': $this->data['object'] = &$data; @@ -665,38 +680,38 @@ $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... - + // 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 + * 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) + * (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 + * @return array of TEMPLATE_Element s * @access public */ - + function getElement($name=false) { - + // load the elements if required: - if (!isset($this->data['elements_original']) && + if (!isset($this->data['elements_original']) && $this->files['elements_file'] &&file_exists($this->files['elements_file']) ) { // maybe more comlexity required? require_once 'HTML/Template/Element.php'; @@ -710,8 +725,8 @@ - /** - * -- consider if this is necessary.. + /** + * -- consider if this is necessary.. * Set one or multiple (array) options * * @access public @@ -727,4 +742,6 @@ $this->options[$name] = $value; } } -} \ No newline at end of file +} + +?> \ No newline at end of file
« previous php.pear.cvs (#20762) next »