HTML_Template_IT
[ class tree: HTML_Template_IT ] [ index: HTML_Template_IT ] [ all elements ]

Class: HTML_Template_IT

Source Location: /HTML_Template_IT-1.1.2/IT.php

Class Overview


Integrated Template - IT


Author(s):

Version:

  • $Id: IT.php,v 1.13 2005/10/28 11:00:33 dufuz Exp $

Variables

Methods


Child classes:

HTML_Template_ITX
Integrated Template Extension - ITX

Inherited Variables

Inherited Methods


Class Details

[line 125]
Integrated Template - IT

Well there's not much to say about it. I needed a template class that supports a single template file with multiple (nested) blocks inside and a simple block API.

The Isotemplate API is somewhat tricky for a beginner although it is the best one you can build. template::parse() [phplib template = Isotemplate] requests you to name a source and a target where the current block gets parsed into. Source and target can be block names or even handler names. This API gives you a maximum of fexibility but you always have to know what you do which is quite unusual for php skripter like me.

I noticed that I do not any control on which block gets parsed into which one. If all blocks are within one file, the script knows how they are nested and in which way you have to parse them. IT knows that inner1 is a child of block2, there's no need to tell him about this.

<table border> <tr> <td colspan=2> __global__ <p> (hidden and automatically added) </td> </tr> <tr> <td>block1</td> <td> <table border> <tr> <td colspan=2>block2</td> </tr> <tr> <td>inner1</td> <td>inner2</td> </tr> </table> </td> </tr> </table>

To add content to block1 you simply type:

  1. $tpl->setCurrentBlock("block1");
and repeat this as often as needed:
  1.    $tpl->setVariable(...);
  2.    $tpl->parseCurrentBlock();

To add content to block2 you would type something like:

  1.  $tpl->setCurrentBlock("inner1");
  2.  $tpl->setVariable(...);
  3.  
  4.  $tpl->setVariable(...);
  5.  
  6.  $tpl->parse("block1");

This will result in one repition of block1 which contains two repitions of inner1. inner2 will be removed if $removeEmptyBlock is set to true which is the default.

Usage:

  1.  $tpl = new HTML_Template_IT[string filerootdir);
  2.  
  3.  // load a template or set it with setTemplate()
  4.  $tpl->loadTemplatefilestring filename [boolean removeUnknownVariablesboolean removeEmptyBlocks)
  5.  
  6.  // set "global" Variables meaning variables not beeing within a (inner) block
  7.  $tpl->setVariablestring variablenamemixed value );
  8.  
  9.  // like with the Isotemplates there's a second way to use setVariable()
  10.  $tpl->setVariablearray string varname => mixed value ) );
  11.  
  12.  // Let's use any block, even a deeply nested one
  13.  $tpl->setCurrentBlockstring blockname );
  14.  
  15.  // repeat this as often as you need it.
  16.  $tpl->setVariablearray string varname => mixed value ) );
  17.  
  18.  // get the parsed template or print it: $tpl->show()
  19.  $tpl->get();

  • Author: Ulf Wendel <uw@netuse.de>
  • Version: $Id: IT.php,v 1.13 2005/10/28 11:00:33 dufuz Exp $
  • Access: public


[ Top ]


Class Variables

$blockdata = array()

[line 237]

Array with the parsed content of a block.

Type:   array


[ Top ]

$blockinner = array()

[line 249]

Array of inner blocks of a block.

Type:   array


[ Top ]

$blocklist = array()

[line 230]

Array of all blocks and their content.

Type:   array


[ Top ]

$blocknameRegExp =  '[0-9A-Za-z_-]+'

[line 165]

RegExp matching a block in the template.

Per default "sm" is used as the regexp modifier, "i" is missing. That means a case sensitive search is done.


Type:   string


[ Top ]

$blockRegExp =  ''

[line 210]

RegExp used to find blocks an their content, filled by the constructor.
  • See: IntegratedTemplate()

Type:   string


[ Top ]

$blockvariables = array()

[line 243]

Array of variables in a block.

Type:   array


[ Top ]

$clearCache =  false

[line 139]

Clear cache on get()?

Type:   boolean


[ Top ]

$clearCacheOnParse =  false

[line 306]

Clear the variable cache on parse?

If you're not an expert just leave the default false. True reduces memory consumption somewhat if you tend to add lots of values for unknown placeholder.


Type:   boolean


[ Top ]

$closingDelimiter =  '}'

[line 155]

Last character of a variable placeholder ( {VARIABLE_}_ ).

Type:   string


[ Top ]

$currentBlock =  '__global__'

[line 216]

Name of the current block.

Type:   string


[ Top ]

$err = array()

[line 133]

Contains the error objects
  • See: halt(), $printError, $haltOnError
  • Access: public

Type:   array


[ Top ]

$fileRoot =  ''

[line 314]

Root directory for all file operations.

The string gets prefixed to all filenames given.


Type:   string


[ Top ]

$flagBlocktrouble =  false

[line 320]

Internal flag indicating that a blockname was used multiple times.

Type:   boolean


[ Top ]

$flagCacheTemplatefile =  true

[line 340]

EXPERIMENTAL! FIXME! Flag indication that a template gets cached.

Complex templates require some times to be preparsed before the replacement can take place. Often I use one template file over and over again but I don't know before that I will use the same template file again. Now IT could notice this and skip the preparse.


Type:   boolean


[ Top ]

$flagGlobalParsed =  false

[line 326]

Flag indicating that the global block was parsed.

Type:   boolean


[ Top ]

$lastTemplatefile =  ''

[line 345]

EXPERIMENTAL! FIXME!

Type:   mixed


[ Top ]

$openingDelimiter =  '{'

[line 147]

First character of a variable placeholder ( _{_VARIABLE} ).

Type:   string


[ Top ]

$removeEmptyBlocks =  true

[line 203]

Controls the handling of empty blocks, default is remove.
  • Access: public

Type:   boolean


[ Top ]

$removeUnknownVariables =  true

[line 196]

Controls the handling of unknown variables, default is remove.
  • Access: public

Type:   boolean


[ Top ]

$removeVariablesRegExp =  ''

[line 189]

RegExp used to strip unused variable placeholder.

Type:   mixed


[ Top ]

$template =  ''

[line 222]

Content of the template.

Type:   string


[ Top ]

$touchedBlocks = array()

[line 276]

List of blocks to preverse even if they are "empty".

This is something special. Sometimes you have blocks that should be preserved although they are empty (no placeholder replaced). Think of a shopping basket. If it's empty you have to drop a message to the user. If it's filled you have to show the contents of the shopping baseket. Now where do you place the message that the basket is empty? It's no good idea to place it in you applications as customers tend to like unecessary minor text changes. Having another template file for an empty basket means that it's very likely that one fine day the filled and empty basket templates have different layout. I decided to introduce blocks that to not contain any placeholder but only text such as the message "Your shopping basked is empty".

Now if there is no replacement done in such a block the block will be recognized as "empty" and by default ($removeEmptyBlocks = true) be stripped off. To avoid thisyou can now call touchBlock() to avoid this.

The array $touchedBlocks stores a list of touched block which must not be removed even if they are empty.


Type:   array


[ Top ]

$variableCache = array()

[line 295]

Variable cache.

Variables get cached before any replacement is done. Advantage: empty blocks can be removed automatically. Disadvantage: might take some more memory


Type:   array


[ Top ]

$variablenameRegExp =  '[0-9A-Za-z_-]+'

[line 175]

RegExp matching a variable placeholder in the template.

Per default "sm" is used as the regexp modifier, "i" is missing. That means a case sensitive search is done.


Type:   string


[ Top ]

$variablesRegExp =  ''

[line 183]

RegExp used to find variable placeholder, filled by the constructor.
  • Var: Looks somewhat like @(delimiter varname delimiter)@
  • See: IntegratedTemplate()
  • Access: public

Type:   string


[ Top ]



Method Detail

HTML_Template_IT (Constructor)   [line 371]

HTML_Template_IT HTML_Template_IT( [string $root = ''], [ $options = null])

Builds some complex regular expressions and optinally sets the file root directory.

Make sure that you call this constructor if you derive your template class from this one.


Parameters:

string   $root   —  File root directory, prefix for all filenames given to the object.
   $options   — 

[ Top ]

buildBlockvariablelist   [line 809]

void buildBlockvariablelist( )

Build a list of all variables within of a block

[ Top ]

errorMessage   [line 965]

string errorMessage( integer $value, [ $blockname = ''])

Return a textual error message for a IT error code
  • Return: error message, or false if the error code was not recognized

Parameters:

integer   $value   —  error code
   $blockname   — 

[ Top ]

findBlocks   [line 850]

void findBlocks( string $string)

Recusively builds a list of all blocks within the template.

Parameters:

string   $string   —  string that gets scanned

[ Top ]

free   [line 707]

void free( )

Clears all datafields of the object.

Don't use this function unless you know what you're doing.


[ Top ]

get   [line 455]

string get( [string $block = '__global__'])

Returns a block with all replacements done.

Parameters:

string   $block   —  name of the block

[ Top ]

getFile   [line 902]

string getFile( string $filename)

Reads a file from disk and returns its content.
  • Return: Filecontent

Parameters:

string   $filename   —  Filename

[ Top ]

getGlobalvariables   [line 827]

void getGlobalvariables( )

Returns a list of all global variables

[ Top ]

init   [line 690]

void init( )

Clears all datafields of the object and rebuild the internal blocklist

LoadTemplatefile() and setTemplate() automatically call this function when a new template is given. Don't use this function unless you know what you're doing.


Overridden in child classes as:

HTML_Template_ITX::init()

[ Top ]

loadTemplatefile   [line 768]

boolean loadTemplatefile( string $filename, [bool $removeUnknownVariables = true], [bool $removeEmptyBlocks = true])

Reads a template file from the disk.

Parameters:

string   $filename   —  name of the template file
bool   $removeUnknownVariables   —  how to handle unknown variables.
bool   $removeEmptyBlocks   —  how to handle empty blocks.

[ Top ]

parse   [line 497]

void parse( [string $block = '__global__'], [ $flag_recursion = false])

Parses the given block.

Parameters:

string   $block   —  name of the block to be parsed
   $flag_recursion   — 

[ Top ]

parseCurrentBlock   [line 604]

void parseCurrentBlock( )

Parses the current block

[ Top ]

setCurrentBlock   [line 643]

boolean setCurrentBlock( [string $block = '__global__'])

Sets the name of the current block that is the block where variables are added.
  • Return: false on failure, otherwise true
  • Throws: PEAR_Error
  • Access: public

Parameters:

string   $block   —  name of the block

[ Top ]

setOption   [line 398]

mixed setOption( string $option, mixed $value)

Sets the option for the template class
  • Return: IT_OK on success, error object on failure
  • Access: public

Parameters:

string   $option   —  option name
mixed   $value   —  option value

[ Top ]

setOptions   [line 423]

mixed setOptions( string $options, mixed 1)

Sets the options for the template class
  • Return: IT_OK on success, error object on failure
  • See: $options
  • Access: public

Parameters:

mixed   1   —  option value
string   $options   —  options array of options default value: 'preserve_data' => false, 'use_preg' => true

[ Top ]

setRoot   [line 797]

void setRoot( string $root)

Sets the file root. The file root gets prefixed to all filenames passed to the object.

Make sure that you override this function when using the class on windows.

  • See: IntegratedTemplate()
  • Access: public

Parameters:

string   $root   — 

[ Top ]

setTemplate   [line 733]

void setTemplate( string $template, [boolean $removeUnknownVariables = true], [boolean $removeEmptyBlocks = true])

Sets the template.

You can eighter load a template file from disk with LoadTemplatefile() or set the template manually using this function.


Parameters:

string   $template   —  template content
boolean   $removeUnknownVariables   —  remove unknown/unused variables?
boolean   $removeEmptyBlocks   —  remove empty blocks?

[ Top ]

setVariable   [line 623]

void setVariable( mixed $variable, [string $value = ''], string 2)

Sets a variable value.

The function can be used eighter like setVariable( "varname", "value") or with one array $variables["varname"] = "value" given setVariable($variables) quite like phplib templates set_var().

  • Access: public

Parameters:

string   2   —  prefix for variable names
mixed   $variable   —  string with the variable name or an array %variables["varname"] = "value"
string   $value   —  value of the variable or empty if $variable is an array.

[ Top ]

show   [line 441]

void show( [ $block = '__global__'])

Print a certain block with all replacements done.

Parameters:

   $block   — 

[ Top ]

touchBlock   [line 667]

boolean touchBlock( string $block)

Preserves an empty block even if removeEmptyBlocks is true.

Parameters:

string   $block   —  name of the block

[ Top ]


Documentation generated on Mon, 11 Mar 2019 14:11:06 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.