Class Template

Description

Template processor class

This class is the primary interface to deal with template files: text files that can be transformed by a template engine. The template engine provided by PHP2Go supports basic templating tools, like variables substitution and include support, and advanced tools, such as condition/iteration tags, support for function calls, capture areas, nested repetition blocks.

To read more about the pattern understood by the template parser, please consult the examples included in the framework's distribution.

Located in /core/template/Template.class.php (line 56)

PHP2Go
   |
   --Component
      |
      --Template
Direct descendents
Class Description
DocumentElement Extends base template class adding buffering capability and some utility methods
Variable Summary
Method Summary
Template Template (string $source, [int $type = T_BYFILE])
void addModifier (string $name, array $spec)
void assign (string|array $variable, [mixed $value = ''])
void assignByRef (string $variable, mixed &$value)
void createAndAssign (string $blockName, string|array $variable, [mixed $value = ''])
void createBlock (string $block)
void display ()
string getContent ()
array getDefinedBlocks ()
array|bool getDefinedVariables ([string $blockName = NULL])
mixed getValue (string $variable)
void globalAssign (string|array $variable, [mixed $value = ''])
void includeAssign (string $blockName, string $value, [int $type = T_BYFILE])
bool isBlockDefined (string $block)
bool isPrepared ()
bool isVariableDefined (string $variable)
void onPreRender ()
void parse ()
void resetTemplate ()
void setCacheProperties (string $dir, [int $lifeTime = NULL], [bool $useMTime = TRUE])
void setCurrentBlock (string $block)
void setTagDelimiter (int $type)
void _assign (string $variable, mixed $value)
string _getFullPath (string $block, string $variable)
array &_getLastInstance (string $blockName)
mixed _getLoopItem (array|object  &$loop, [bool $returnKey = FALSE])
int _getLoopTotal (array|object  &$loop)
void _globalAssign (string $variable, mixed $value)
void _loadConfigVars (array $props)
void _loadGlobalSettings (array $settings)
array &_popStack (array &$stack, string &$blockName, int &$instance, int &$instanceCount)
void _pushStack (array &$stack, string $blockName, array &$block, int $instance, int $instanceCount)
Variables
array $blockIndex = array() (line 88)

Holds the next instance number for all dynamic blocks already instantiated

  • access: private
array $cacheOptions = array() (line 64)

Cache options

  • access: private
TemplateConfigFile $ConfigLoader = NULL (line 161)

TemplateConfigFile instance

  • access: private
array $currentBlock = NULL (line 80)

Points to the current dynamic block

  • access: private
string $currentBlockName (line 72)

Current block name (defaults to TP_ROOTBLOCK)

  • access: private
TemplateParser $Parser = NULL (line 153)

Parser instance

  • access: private
array $tplCapture = array() (line 120)

Capture control variables

  • access: private
array $tplComponents = array() (line 128)

Registered components

  • access: private
array $tplConfigVars = array(0 => array()) (line 96)

Config variables

  • access: private
array $tplContent = array() (line 137)

Control structure that holds template's dynamic content: block instances and variables

  • access: private
array $tplGlobalVars = array() (line 104)

Global template variables

  • access: private
array $tplInternalVars = array() (line 112)

Internal template variables

  • access: private
int $tplMTime (line 145)

Modified time of the template's source

  • access: private

Inherited Variables

Inherited from Component

Component::$attributes
Component::$preRendered
Methods
Constructor Template (line 170)

Class constructor

Template Template (string $source, [int $type = T_BYFILE])
  • string $source: Template source (string or file name)
  • int $type: Source type (T_BYFILE or T_BYVAR)
addModifier (line 255)

Register a custom variable modifier

Modifiers can be specified in 3 different ways:

  • 'function_name' or array('function_name'): a procedural function from an already included file
  • array('class', 'method'): a static method from an already included class
  • array('path.to.the.class', 'class', 'method'): a static method from a class that should be imported (path is provided as the first array entry)
This method can't be used to replace modifiers bundled with PHP2Go.

void addModifier (string $name, array $spec)
  • string $name: Modifier name
  • array $spec: Modifier spec
assign (line 548)

Assigns a variable

The $variable argument can be a hashmap of variables and values, a simple variable name (and so, must be present in the current active block) or a 'block.variable' expression, which allows to assign variables on the most recent instance of a different block (not the current one).

void assign (string|array $variable, [mixed $value = ''])
  • string|array $variable: Variable name or path or hashmap or variables and values
  • mixed $value: Variable value
assignByRef (line 566)

Assigns a variable by reference

The $variable argument can be a variable name or a 'block.variable' expression.

void assignByRef (string $variable, mixed &$value)
  • string $variable: Variable name
  • mixed &$value: Variable reference
createAndAssign (line 531)

Creates a new instance of a given block and assigns a variable or a set of variables

void createAndAssign (string $blockName, string|array $variable, [mixed $value = ''])
  • string $blockName: Block name
  • string|array $variable: Variable name or hashmap of variables and values
  • mixed $value: Variable value
createBlock (line 466)

Creates a new instance of a dynamic block

The dynamic block must be declared in the template source using the following syntax:

  1.  <!-- start block block_name -->
  2.  <!-- end block block_name -->

The recently created block is transformed into the active block, so that all subsequent assign operations will be applied on it

void createBlock (string $block)
  • string $block: Block name
display (line 673)

Builds and displays the template's output code

void display ()

Redefinition of:
Component::display()
Abstract method that should print the component's output
getContent (line 662)

Builds and returns the template's output code

string getContent ()

Redefinition of:
Component::getContent()
Get the output produced by this component
getCurrentBlockName (line 447)

Get the name of the active block

string getCurrentBlockName ()
getDefinedBlocks (line 304)

Get all declared blocks

Returns FALSE when the template wasn't compiled yet.

array getDefinedBlocks ()
getDefinedVariables (line 364)

Get all variables defined in a given block

If $blockName is missing, TP_ROOTBLOCK will be used.

Returns FALSE when the template wasn't compiled yet.

array|bool getDefinedVariables ([string $blockName = NULL])
  • string $blockName: Block name
getValue (line 430)

Get the current value of a variable

Just as isVariableDefined, this method accepts a 'block.variable' expression in the $variable argument.

mixed getValue (string $variable)
  • string $variable: Variable name or path
globalAssign (line 592)

Assigns a global template variable

Global template variables are available in all scopes. This means that, no matter which is the current block, you'll be able to use it.

void globalAssign (string|array $variable, [mixed $value = ''])
  • string|array $variable: Variable name or hashmap of variables and values
  • mixed $value: Variable value
includeAssign (line 623)

Assigns an include block

The assignment of include blocks must be done before the parse() method is called.

Example:

  1.  /* tpl file */
  2.  <!-- include block include1 -->
  3.  <!-- include block include2 -->
  4.  /* php file */
  5.  $tpl new Template('templates/my_template.tpl');
  6.  $tpl->includeAssign('include1''templates/my_include1.tpl'T_BYFILE);
  7.  $tpl->includeAssign('include2''templates/my_include2.tpl'T_BYFILE);
  8.  $tpl->parse();

void includeAssign (string $blockName, string $value, [int $type = T_BYFILE])
  • string $blockName: Include block name
  • string $value: Include block contents (file path or variable)
  • int $type: Content type(T_BYFILE or T_BYVAR)
isBlockDefined (line 333)

Checks if a given block name is defined in the template source

The $block argument can be either a block name or a path in the blocks tree. Examples:

  1.  /* tpl file */
  2.  <!-- start block row -->
  3.  <tr>
  4.  <!-- start block column -->
  5.    <td>{$data}</td>
  6.  <!-- end block column -->
  7.  </tr>
  8.  <!-- end block row -->
  9.  /* php file */
  10.  $tpl new Template('my_template.tpl');
  11.  $def $tpl->isBlockDefined('row');
  12.  $def2 $tpl->isBlockDefined('row.column');

bool isBlockDefined (string $block)
  • string $block: Block name
isPrepared (line 293)

Checks if the template was already compiled

bool isPrepared ()
isVariableDefined (line 404)

Checks if a given variable name is declared

The $variable argument can be either a variable name or a 'block.variable' expression. Examples:

  1.  /* tpl file */
  2.  {$title}
  3.  <!-- start block row -->
  4.  <tr>
  5.    <td>{$line}</td>
  6.  <!-- start block column -->
  7.    <td>{$data}</td>
  8.  <!-- end block column -->
  9.  </tr>
  10.  <!-- end block row -->
  11.  /* php file */
  12.  $tpl new Template('my_template.tpl');
  13.  $tpl->parse();
  14.  $def $tpl->isVariableDefined('title');
  15.  $def2 $tpl->isVariableDefined('row.line');
  16.  $def3 $tpl->isVariableDefined('column.data');

bool isVariableDefined (string $variable)
  • string $variable: Variable name or path
onPreRender (line 639)

Prepares the template to be rendered

Automatically called inside getContent and display.

void onPreRender ()

Redefinition of:
Component::onPreRender()
Pre-render the component
parse (line 271)

Triggers the compilation of the template

This method must be called manually before the template is populated with content (variables, blocks). If cache is enabled, a previously compiled template will be loaded from the cache storage.

This method must be called only once.

void parse ()

Redefined in descendants as:
resetTemplate (line 282)

Returns the template to the state immediately after the compilation

All assigned variables are cleared and all block instances are destroyed.

void resetTemplate ()
setCacheProperties (line 196)

Enable cache and configure cache properties

void setCacheProperties (string $dir, [int $lifeTime = NULL], [bool $useMTime = TRUE])
  • string $dir: Cache base dir
  • int $lifeTime: Cache lifetime in seconds
  • bool $useMTime: Whether to control cache based on original source's modified time
setCurrentBlock (line 516)

Changes the internal pointer to a given block name

Example:

  1.  /* tpl file */
  2.  {$out}
  3.  <!-- start block internal -->
  4.  {$var}
  5.  <!-- end block internal -->
  6.  /* php file */
  7.  $tpl new Template('my_template.php');
  8.  $tpl->parse();
  9.  $tpl->createBlock('internal');
  10.  $tpl->assign('var''blah');
  11.  $tpl->assign('out''Hello World!');

void setCurrentBlock (string $block)
  • string $block: Block name
setTagDelimiter (line 228)

Set the tag delimiter type

This template engine currently supports 3 types of tag delimiters:

  • TEMPLATE_DELIM_COMMENT: tags are surrounded by HTML comments.
    1.  <!-- if $var eq -->
    2.  <!-- loop var=$data item="item" -->
    3.  <!-- end if -->
  • TEMPLATE_DELIM_BRACE: tags are surrounded by curly braces
    1.  {if $var eq 1}
    2.  {loop var=$data item="item"}
  • TEMPLATE_DELIM_DIRECTIVE: tags are surrounded by directive delimiters
    1.  <%if $var eq 1%>
    2.  <%loop var=$data item="item"%>
    3.  <%end if%>

void setTagDelimiter (int $type)
  • int $type: Tag delimiter type
_assign (line 706)

Internal method used to assign variables

  • access: private
void _assign (string $variable, mixed $value)
  • string $variable: Variable name
  • mixed $value: Variable value
_getFullPath (line 758)

Builds a full path of a variable, considering block index, block name, block instance and variable name

  • access: private
string _getFullPath (string $block, string $variable)
  • string $block: Block name
  • string $variable: Variable name
_getLastInstance (line 742)

Given a block name, returns its last instance

  • access: private
array &_getLastInstance (string $blockName)
  • string $blockName
_getLoopItem (line 836)

Get the next item of a given loop

The loop can be an array, a hashmap, a DataSet instance or an ADORecordSet instance. For each type, there's a different way to fetch and return the next record.

  • access: private
mixed _getLoopItem (array|object  &$loop, [bool $returnKey = FALSE])
  • array|object &$loop: Loop
  • bool $returnKey: Whether to return an array containing key and value or just the value
_getLoopTotal (line 809)

Calculate the total iterations of a given loop

  • access: private
int _getLoopTotal (array|object  &$loop)
  • array|object &$loop
_globalAssign (line 729)

Internal method used to assign global variables

  • access: private
void _globalAssign (string $variable, mixed $value)
  • string $variable: Variable name
  • mixed $value: Variable value
_initializeContent (line 684)

Initializes the internal control variables

  • access: private
void _initializeContent ()
_loadConfigVars (line 864)

Load a set of config variables

void _loadConfigVars (array $props)
  • array $props: Properties from the CONFIG tag
_loadGlobalSettings (line 893)

Loads global configuration settings

  • access: private
void _loadGlobalSettings (array $settings)
  • array $settings: Settings
_popStack (line 793)

Used at runtime to pop a dynamic block from the block stack

  • return: Block data
  • access: private
array &_popStack (array &$stack, string &$blockName, int &$instance, int &$instanceCount)
  • array &$stack: Block stack
  • string &$blockName: Block name
  • int &$instance: Current instance
  • int &$instanceCount: Total instances
_pushStack (line 774)

Used at runtime to push a dynamic block onto the block stack

  • access: private
void _pushStack (array &$stack, string $blockName, array &$block, int $instance, int $instanceCount)
  • array &$stack: Block stack
  • string $blockName: Block name
  • array &$block: Block data
  • int $instance: Current block instance
  • int $instanceCount: Total instances

Inherited Methods

Inherited From Component

Component::Component()
Component::display()
Component::getAttribute()
Component::getContent()
Component::hasAttribute()
Component::onPreRender()
Component::setAttribute()
Component::__destruct()
Component::__toString()

Inherited From PHP2Go

PHP2Go::PHP2Go()
PHP2Go::equals()
PHP2Go::generateUniqueId()
PHP2Go::getClassName()
PHP2Go::getConfigVal()
PHP2Go::getLangVal()
PHP2Go::getObjectName()
PHP2Go::getParentName()
PHP2Go::hasDestructor()
PHP2Go::hashCode()
PHP2Go::isA()
PHP2Go::isSubclassOf()
PHP2Go::logError()
PHP2Go::raiseError()
PHP2Go::registerDestructor()
PHP2Go::registerShutdownFunc()
PHP2Go::retrieve()
PHP2Go::store()
PHP2Go::__toString()

Documentation generated on Sun, 07 Oct 2007 19:30:12 -0300 by phpDocumentor 1.3.2