Class Savant3

Description

Provides an object-oriented template system for PHP5.

Savant3 helps you separate business logic from presentation logic using PHP as the template language. By default, Savant3 does not compile templates. However, you may pass an optional compiler object to compile template source to include-able PHP code. It is E_STRICT compliant for PHP5.

Please see the documentation at http://phpsavant.com/, and be sure to donate! :-)

Located in /Savant3.php (line 46)


	
			
Variable Summary
array $__config
Method Summary
object Savant3 __construct ([array $config = null])
void addEscape ()
void addFilters (array 0)
void addPath ( $type, string|array $path)
string apiVersion ()
void applyFilters (string $buffer)
bool assign ()
bool assignRef ( $key,  &$val)
void display ([string $tpl = null])
void eprint (mixed $value)
object Savant3_Error error (string $code, [array $info = array()], [int $level = E_USER_ERROR], [bool $trace = true])
mixed escape (mixed $value)
mixed fetch ([string $tpl = null])
string|bool findFile (array $type, string $file)
mixed getConfig ([string $key = null])
array getEscape ()
boolean isError (object $obj)
mixed plugin (string $name)
void setAutoload (bool $flag)
void setCompiler (mixed $compiler)
void setErrorText (string $text)
void setEscape ()
void setExceptions (bool $flag)
void setExtract (bool $flag)
void setFilters (array 0)
void setPath (string $type, string|array $path)
void setPluginConf (string $plugin, [array $config = null])
void setTemplate (string $template)
string template ([string $tpl = null])
mixed __call (string $func, array $args)
string __toString ([string $tpl = null])
Variables
array $__config = array(
'template_path' => array(),'resource_path'=>array(),'error_text'=>"\n\ntemplate error, examine fetch() result\n\n",'exceptions'=>false,'autoload'=>false,'compiler'=>null,'filters'=>array(),'plugins'=>array(),'template'=>null,'plugin_conf'=>array(),'extract'=>false,'fetch'=>null,'escape'=>array('htmlspecialchars'),)
(line 59)

Array of configuration parameters.

  • access: protected
Methods
Constructor __construct (line 96)

Constructor.

  • return: A Savant3 instance.
  • access: public
object Savant3 __construct ([array $config = null])
  • array $config: An associative array of configuration keys for the Savant3 object. Any, or none, of the keys may be set.
addEscape (line 554)

Adds to the callbacks used when calling $this->escape().

Each parameter passed to this function is treated as a separate callback. For example:

  1.  $savant->addEscape(
  2.      'stripslashes',
  3.      'htmlspecialchars',
  4.      array('StaticClass''method'),
  5.      array($object$method)
  6.  );

  • access: public
void addEscape ()
addFilters (line 1182)

Adds filter callbacks to the stack of filters.

  • access: public
void addFilters (array 0)
  • array 0: An array of filter callbacks.
addPath (line 760)

Adds to the search path for templates and resources.

  • access: public
void addPath ( $type, string|array $path)
  • string|array $path: The directory or stream to search.
  • $type
apiVersion (line 254)

Reports the API version for this class.

  • return: A PHP-standard version number.
  • access: public
string apiVersion ()
applyFilters (line 1204)

Runs all filter callbacks on buffered output.

  • access: protected
void applyFilters (string $buffer)
  • string $buffer: The template output.
assign (line 905)

Sets variables for the template (by copy).

This method is overloaded; you can assign all the properties of an object, an associative array, or a single value by name.

You are not allowed to assign any variable named '__config' as it would conflict with internal configuration tracking.

In the following examples, the template will have two variables assigned to it; the variables will be known inside the template as "$this->var1" and "$this->var2".

  1.  $Savant3 new Savant3();
  2.  
  3.  // assign by object
  4.  $obj new stdClass;
  5.  $obj->var1 'something';
  6.  $obj->var2 'else';
  7.  $Savant3->assign($obj);
  8.  
  9.  // assign by associative array
  10.  $ary array('var1' => 'something''var2' => 'else');
  11.  $Savant3->assign($ary);
  12.  
  13.  // assign by name and value
  14.  $Savant3->assign('var1''something');
  15.  $Savant3->assign('var2''else');
  16.  
  17.  // assign directly
  18.  $Savant3->var1 'something';
  19.  $Savant3->var2 'else';

  • return: True on success, false on failure.
  • access: public
bool assign ()
assignRef (line 968)

Sets variables for the template (by reference).

You are not allowed to assign any variable named '__config' as it would conflict with internal configuration tracking.

  1.  $Savant3 new Savant3();
  2.  
  3.  // assign by name and value
  4.  $Savant3->assignRef('var1'$ref);
  5.  
  6.  // assign directly
  7.  $Savant3->ref =$var1;

  • return: True on success, false on failure.
  • access: public
bool assignRef ( $key,  &$val)
  • $key
  • &$val
display (line 999)

Displays a template directly (equivalent to

  1. echo 
).

  • access: public
void display ([string $tpl = null])
  • string $tpl: The template source to compile and display.
eprint (line 684)

Prints a value after escaping it for output.

You can override the predefined escaping callbacks by passing added parameters as replacement callbacks.

  1.  // use predefined callbacks
  2.  $this->eprint($value);
  3.  
  4.  // use replacement callbacks
  5.  $this->eprint(
  6.      $value,
  7.      'stripslashes',
  8.      'htmlspecialchars',
  9.      array('StaticClass''method'),
  10.      array($object$method)
  11.  );

  • access: public
void eprint (mixed $value)
  • mixed $value: The value to be escaped and printed.
error (line 1259)

Returns an error object or throws an exception.

  • access: public
object Savant3_Error error (string $code, [array $info = array()], [int $level = E_USER_ERROR], [bool $trace = true])
  • string $code: A Savant3 'ERR_*' string.
  • array $info: An array of error-specific information.
  • int $level: The error severity level, default is E_USER_ERROR (the most severe possible).
  • bool $trace: Whether or not to include a backtrace, default true.
escape (line 615)

Applies escaping to a value.

You can override the predefined escaping callbacks by passing added parameters as replacement callbacks.

  1.  // use predefined callbacks
  2.  $result $savant->escape($value);
  3.  
  4.  // use replacement callbacks
  5.  $result $savant->escape(
  6.      $value,
  7.      'stripslashes',
  8.      'htmlspecialchars',
  9.      array('StaticClass''method'),
  10.      array($object$method)
  11.  );

Unfortunately, a call to "echo htmlspecialchars()" is twice as fast as a call to "echo $this->escape()" under the default escaping (which is htmlspecialchars). The benchmark showed 0.007 seconds for htmlspecialchars(), and 0.014 seconds for $this->escape(), on 300 calls each.

  • access: public
mixed escape (mixed $value)
  • mixed $value: The value to be escaped.
fetch (line 1018)

Compiles, executes, and filters a template source.

  • return: The template output string, or a Savant3_Error.
  • access: public
mixed fetch ([string $tpl = null])
  • string $tpl: The template to process; if null, uses the default template set with setTemplate().
findFile (line 819)

Searches the directory paths for a given file.

  • return: The full path and file name for the target file, or boolean false if the file is not found in any of the paths.
  • access: protected
string|bool findFile (array $type, string $file)
  • array $type: The type of path to search (template or resource).
  • string $file: The file name to look for.
getConfig (line 347)

Returns a copy of the Savant3 configuration parameters.

  • return: A copy of the $this->__config array.
  • access: public
mixed getConfig ([string $key = null])
  • string $key: The specific configuration key to return. If null, returns the entire configuration array.
getEscape (line 573)

Gets the array of output-escaping callbacks.

  • return: The array of output-escaping callbacks.
  • access: public
array getEscape ()
isError (line 1305)

Tests if an object is of the Savant3_Error class.

  • return: True if $obj is an error object of the type Savant3_Error, or is a subclass that Savant3_Error. False if not.
  • access: public
boolean isError (object $obj)
  • object $obj: The object to be tested.
plugin (line 274)

Returns an internal plugin object; creates it as needed.

  • return: The plugin object, or a Savant3_Error with an ERR_PLUGIN code if it can't find the plugin.
  • access: public
mixed plugin (string $name)
  • string $name: The plugin name. If this plugin has not been created yet, this method creates it automatically.
setAutoload (line 374)

Sets __autoload() usage on or off.

  • access: public
void setAutoload (bool $flag)
  • bool $flag: True to use __autoload(), false to not use it.
setCompiler (line 397)

Sets a custom compiler/pre-processor callback for template sources.

By default, Savant3 does not use a compiler; use this to set your own custom compiler (pre-processor) for template sources.

  • access: public
void setCompiler (mixed $compiler)
  • mixed $compiler: A compiler callback value suitable for the first parameter of call_user_func(). Set to null/false/empty to use PHP itself as the template markup (i.e., no compiling).
setErrorText (line 415)

Sets the custom error text for __toString().

  • access: public
void setErrorText (string $text)
  • string $text: The error text when a template is echoed.
setEscape (line 526)

Clears then sets the callbacks to use when calling $this->escape().

Each parameter passed to this function is treated as a separate callback. For example:

  1.  $savant->setEscape(
  2.      'stripslashes',
  3.      'htmlspecialchars',
  4.      array('StaticClass''method'),
  5.      array($object$method)
  6.  );

  • access: public
void setEscape ()
setExceptions (line 434)

Sets whether or not exceptions will be thrown.

  • access: public
void setExceptions (bool $flag)
  • bool $flag: True to turn on exception throwing, false to turn it off.
setExtract (line 453)

Sets whether or not variables will be extracted.

  • access: public
void setExtract (bool $flag)
  • bool $flag: True to turn on variable extraction, false to turn it off.
setFilters (line 1164)

Resets the filter stack to the provided list of callbacks.

  • access: public
void setFilters (array 0)
  • array 0: An array of filter callbacks.
setPath (line 726)

Sets an entire array of search paths for templates or resources.

  • access: public
void setPath (string $type, string|array $path)
  • string $type: The type of path to set, typically 'template' or 'resource'.
  • string|array $path: The new set of search paths. If null or false, resets to the current directory only.
setPluginConf (line 473)

Sets config array for a plugin.

  • access: public
void setPluginConf (string $plugin, [array $config = null])
  • string $plugin: The plugin to configure.
  • array $config: The configuration array for the plugin.
setTemplate (line 491)

Sets the template name to use.

  • access: public
void setTemplate (string $template)
  • string $template: The template name.
template (line 1097)

Compiles a template and returns path to compiled script.

By default, Savant does not compile templates, it uses PHP as the markup language, so the "compiled" template is the same as the source template.

Used inside a template script like so:

  1.  include $this->template($tpl);

  • return: The full path to the compiled template script.
  • throws: object An error object with a 'ERR_TEMPLATE' code.
  • access: protected
string template ([string $tpl = null])
  • string $tpl: The template source name to look for.
__call (line 180)

Executes a main plugin method with arbitrary parameters.

  • return: The plugin output, or a Savant3_Error with an ERR_PLUGIN code if it can't find the plugin.
  • access: public
mixed __call (string $func, array $args)
  • string $func: The plugin method name.
  • array $args: The parameters passed to the method.
__toString (line 232)

Magic method to echo this object as template output.

Note that if there is an error, this will output a simple error text string and will not return an error object. Use fetch() to get an error object when errors occur.

  • return: The template output.
  • access: public
string __toString ([string $tpl = null])
  • string $tpl: The template source to use.

Documentation generated on Tue, 31 Mar 2009 16:24:42 -0500 by phpDocumentor 1.4.2