Class MiniTemplator

(line 74)

Located in File: Program_Root/MiniTemplator.class.php


A compact template engine for HTML files.

Requires PHP 4.0.4 or newer.

 Template syntax:

   Variables:
     ${VariableName}

   Blocks:
     <!-- $BeginBlock BlockName -->
     ... block content ...
     <!-- $EndBlock BlockName -->

   Include a subtemplate:
     <!-- $Include RelativeFileName -->
 

 General remarks:
  - Variable names and block names are case-insensitive.
  - The same variable may be used multiple times within a template.
  - Blocks can be nested.
  - Multiple blocks with the same name may occur within a template.
 

 Public methods:
   readTemplateFromFile   - Reads the template from a file.
   setTemplateString      - Assigns a new template string.
   setVariable            - Sets a template variable.
   setVariableEsc         - Sets a template variable to an escaped string value.
   variableExists         - Checks whether a template variable exists.
   addBlock               - Adds an instance of a template block.
   blockExists            - Checks whether a block exists.
   reset                  - Clears all variables and blocks.
   generateOutput         - Generates the HTML page and writes it to the PHP output stream.
   generateOutputToFile   - Generates the HTML page and writes it to a file.
   generateOutputToString - Generates the HTML page and writes it to a string.
 

Home page: http://www.source-code.biz/MiniTemplator
License: This module is released under the GNU/LGPL license (http://www.gnu.org/licenses/lgpl.html).
Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland. All rights reserved.
This product is provided "as is" without warranty of any kind.

Version history:
2001-10-24 Christian d'Heureuse (chdh): VBasic version created.
2002-01-26 Markus Angst: ported to PHP4.
2003-04-07 chdh: changes to adjust to Java version.
2003-07-08 chdh: Method variableExists added. Method setVariable changed to trigger an error when the variable does not exist.
2004-04-07 chdh: Parameter isOptional added to method setVariable. Licensing changed from GPL to LGPL.
2004-04-18 chdh: Method blockExists added.
2004-10-28 chdh:
Method setVariableEsc added.
Multiple blocks with the same name may now occur within a template.
No error ("unknown command") is generated any more, if a HTML comment starts with "${".
2004-11-06 chdh:
"$Include" command implemented.
2004-11-20 chdh:
"$Include" command changed so that the command text is not copied to the output file.


Class Variable Summary ^TOP
$subtemplateBasePath
Base path for relative file names of subtemplates (for the $Include command).
Default Value: -><-

Inherited Class Variable Summary ^TOP

Method Summary ^TOP
void constructor MiniTemplator ( )
Constructs a MiniTemplator object.
boolean addBlock ( $blockName )
Adds an instance of a template block.
boolean blockExists ( $blockName )
Checks whether a block with the specified name exists within the template.
boolean generateOutput ( )
Generates the HTML page and writes it to the PHP output stream.
boolean generateOutputToFile ( $fileName )
Generates the HTML page and writes it to a file.
boolean generateOutputToString ( &$outputString )
Generates the HTML page and writes it to a string.
boolean readTemplateFromFile ( $fileName )
Reads the template from a file.
void reset ( )
Clears all variables and blocks.
boolean setTemplateString ( $templateString )
Assigns a new template string.
boolean setVariable ( $variableName, $variableValue, [$isOptional = false] )
Sets a template variable.
boolean setVariableEsc ( $variableName, $variableValue, [$isOptional = false] )
Sets a template variable to an escaped string.
boolean variableExists ( $variableName )
Checks whether a variable with the specified name exists within the template.
Inherited Method Summary ^TOP

Variable Detail ^TOP

$subtemplateBasePath

(line 84)
Data type: mixed
Base path for relative file names of subtemplates (for the $Include command).

This path is prepended to the subtemplate file names. It must be set before readTemplateFromFile or setTemplateString.



Method Detail ^TOP

Constructor MiniTemplator (line 163)
Usage : void  MiniTemplator( )

Description : Constructs a MiniTemplator object.
Function Info:
access - public

addBlock (line 605)
Usage : boolean  addBlock( string $blockName)

Description : Adds an instance of a template block.

If the block contains variables, these variables must be set before the block is added. If the block contains subblocks (nested blocks), the subblocks must be added before this block is added. If multiple blocks exist with the specified name, an instance is added for each block occurence.

Function Parameters:
- string $blockName blockName the name of the block to be added.
Function Info:
return - true on success, false on error (e.g. when no block with the specified name exists in the template).
access - public

blockExists (line 656)
Usage : boolean  blockExists( string $blockName)

Description : Checks whether a block with the specified name exists within the template.
Function Parameters:
- string $blockName the name of the block.
Function Info:
return - true if the block exists, or false when no block with the specified name exists in the template.
access - public

generateOutput (line 667)
Usage : boolean  generateOutput( )

Description : Generates the HTML page and writes it to the PHP output stream.
Function Info:
return - true on success, false on error.
access - public

generateOutputToFile (line 678)
Usage : boolean  generateOutputToFile( string $fileName)

Description : Generates the HTML page and writes it to a file.
Function Parameters:
- string $fileName name of the output file.
Function Info:
return - true on success, false on error.
access - public

generateOutputToString (line 694)
Usage : boolean  generateOutputToString( mixed &$outputString, string $outputString)

Description : Generates the HTML page and writes it to a string.
Function Parameters:
- string $outputString variable that receives the contents of the generated HTML page.
Function Info:
return - true on success, false on error.
access - public

readTemplateFromFile (line 174)
Usage : boolean  readTemplateFromFile( string $fileName)

Description : Reads the template from a file.
Function Parameters:
- string $fileName name of the file that contains the template.
Function Info:
return - true on success, false on error.
access - public

reset (line 525)
Usage : void  reset( )

Description : Clears all variables and blocks.

This method can be used to produce another HTML page with the same template. It is faster than creating another MiniTemplator object, because the template does not have to be parsed again. All variable values are cleared and all added block instances are deleted.

Function Info:
access - public

setTemplateString (line 187)
Usage : boolean  setTemplateString( string $templateString)

Description : Assigns a new template string.
Function Parameters:
- string $templateString contents of the template file.
Function Info:
return - true on success, false on error.
access - public

setVariable (line 551)
Usage : boolean  setVariable( string $variableName, string $variableValue, [boolean $isOptional = false])

Description : Sets a template variable.

For variables that are used in blocks, the variable value must be set before addBlock is called.

Function Parameters:
- string $variableName the name of the variable to be set.
- string $variableValue the new value of the variable.
- boolean $isOptional Specifies whether an error should be generated when the variable does not exist in the template. If $isOptional is false and the variable does not exist, an error is generated.
Function Info:
return - true on success, or false on error (e.g. when no variable with the specified name exists in the template and $isOptional is false).
access - public

setVariableEsc (line 578)
Usage : boolean  setVariableEsc( string $variableName, string $variableValue, [boolean $isOptional = false])

Description : Sets a template variable to an escaped string.

This method is identical to (@link setVariable), except that the characters <, >, &, ' and " of variableValue are replaced by their corresponding HTML/XML character entity codes. For variables that are used in blocks, the variable value must be set before addBlock is called.

Function Parameters:
- string $variableName the name of the variable to be set.
- string $variableValue the new value of the variable. Special HTML/XML characters are escaped.
- boolean $isOptional Specifies whether an error should be generated when the variable does not exist in the template. If $isOptional is false and the variable does not exist, an error is generated.
Function Info:
return - true on success, or false on error (e.g. when no variable with the specified name exists in the template and $isOptional is false).
access - public

variableExists (line 588)
Usage : boolean  variableExists( string $variableName)

Description : Checks whether a variable with the specified name exists within the template.
Function Parameters:
- string $variableName the name of the variable.
Function Info:
return - true if the variable exists, or false when no variable with the specified name exists in the template.
access - public


Documention generated on Sat, 20 Nov 2004 23:54:33 +0100 by phpDocumentor 1.2.0rc1